Commit f0dd3d5080e596cc3f1afbe7e9eb9ffdcf9dd220

Authored by fbormann
1 parent c0be0059

when create a new form, initial data is correct

reports/forms.py
... ... @@ -13,7 +13,6 @@ class ResourceAndTagForm(forms.Form):
13 13 super(ResourceAndTagForm, self).__init__(*args, **kwargs)
14 14 if kwargs.get('initial'):
15 15 initial = kwargs['initial']
16   - print(initial)
17 16 self.fields['resource'].choices = [(resource.id, resource.name) for resource in initial['resource']]
18 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 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 74 fields[0].onchange = function(item){
67 75 //it request all tags associated with that resource
68 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 58  
59 59  
60 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 63 context['resource_tag_formset'] = resourceTagFormSet
64 64 return context
65 65  
... ... @@ -102,7 +102,7 @@ class ReportView(LoginRequiredMixin, generic.FormView):
102 102 for tag in resource.tags.all():
103 103 tags.append(tag)
104 104 resources.append(resource)
105   - resourceTagFormSet = formset_factory(ResourceAndTagForm, extra= 1)
  105 + resourceTagFormSet = formset_factory(ResourceAndTagForm)
106 106 resources_formset = resourceTagFormSet(self.request.POST, initial=[{'resource':resources, 'tag':tags}])
107 107 if form.is_valid() and resources_formset.is_valid():
108 108 self.form_data = form.cleaned_data
... ...