Commit f0dd3d5080e596cc3f1afbe7e9eb9ffdcf9dd220
1 parent
c0be0059
Exists in
master
and in
3 other branches
when create a new form, initial data is correct
Showing
3 changed files
with
12 additions
and
5 deletions
Show diff stats
reports/forms.py
@@ -13,7 +13,6 @@ class ResourceAndTagForm(forms.Form): | @@ -13,7 +13,6 @@ class ResourceAndTagForm(forms.Form): | ||
13 | super(ResourceAndTagForm, self).__init__(*args, **kwargs) | 13 | super(ResourceAndTagForm, self).__init__(*args, **kwargs) |
14 | if kwargs.get('initial'): | 14 | if kwargs.get('initial'): |
15 | initial = kwargs['initial'] | 15 | initial = kwargs['initial'] |
16 | - print(initial) | ||
17 | self.fields['resource'].choices = [(resource.id, resource.name) for resource in initial['resource']] | 16 | self.fields['resource'].choices = [(resource.id, resource.name) for resource in initial['resource']] |
18 | self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']] | 17 | self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']] |
19 | 18 |
reports/templates/reports/create.html
@@ -62,7 +62,15 @@ | @@ -62,7 +62,15 @@ | ||
62 | fields[0].options[fields[0].options.length] = new Option(data.resources[j].name,data.resources[j].id); | 62 | fields[0].options[fields[0].options.length] = new Option(data.resources[j].name,data.resources[j].id); |
63 | 63 | ||
64 | } | 64 | } |
65 | - //Modify tags fields | 65 | + //Set initial tag options |
66 | + $.get("{% url 'subjects:reports:get_tags' %}?resource_id="+fields[0].value, function(data){ | ||
67 | + fields[1].options.length = 0; | ||
68 | + for(var j = 0; j < data.tags.length; j++){ | ||
69 | + fields[1].options[fields[1].options.length] = new Option(data.tags[j].name,data.tags[j].id); | ||
70 | + } | ||
71 | + }); | ||
72 | + | ||
73 | + //Modify tags fields - on resource type value modifies | ||
66 | fields[0].onchange = function(item){ | 74 | fields[0].onchange = function(item){ |
67 | //it request all tags associated with that resource | 75 | //it request all tags associated with that resource |
68 | $.get("{% url 'subjects:reports:get_tags' %}?resource_id="+item.target.value, function(data){ | 76 | $.get("{% url 'subjects:reports:get_tags' %}?resource_id="+item.target.value, function(data){ |
reports/views.py
@@ -58,8 +58,8 @@ class ReportView(LoginRequiredMixin, generic.FormView): | @@ -58,8 +58,8 @@ class ReportView(LoginRequiredMixin, generic.FormView): | ||
58 | 58 | ||
59 | 59 | ||
60 | #set formset | 60 | #set formset |
61 | - resourceTagFormSet = formset_factory(ResourceAndTagForm, extra = 1) | ||
62 | - resourceTagFormSet = resourceTagFormSet(initial=[{'resource':resources, 'tag':tags}]) | 61 | + resourceTagFormSet = formset_factory(ResourceAndTagForm) |
62 | + resourceTagFormSet = resourceTagFormSet() | ||
63 | context['resource_tag_formset'] = resourceTagFormSet | 63 | context['resource_tag_formset'] = resourceTagFormSet |
64 | return context | 64 | return context |
65 | 65 | ||
@@ -102,7 +102,7 @@ class ReportView(LoginRequiredMixin, generic.FormView): | @@ -102,7 +102,7 @@ class ReportView(LoginRequiredMixin, generic.FormView): | ||
102 | for tag in resource.tags.all(): | 102 | for tag in resource.tags.all(): |
103 | tags.append(tag) | 103 | tags.append(tag) |
104 | resources.append(resource) | 104 | resources.append(resource) |
105 | - resourceTagFormSet = formset_factory(ResourceAndTagForm, extra= 1) | 105 | + resourceTagFormSet = formset_factory(ResourceAndTagForm) |
106 | resources_formset = resourceTagFormSet(self.request.POST, initial=[{'resource':resources, 'tag':tags}]) | 106 | resources_formset = resourceTagFormSet(self.request.POST, initial=[{'resource':resources, 'tag':tags}]) |
107 | if form.is_valid() and resources_formset.is_valid(): | 107 | if form.is_valid() and resources_formset.is_valid(): |
108 | self.form_data = form.cleaned_data | 108 | self.form_data = form.cleaned_data |