django - Combining unrelated Models in Formset and saving results -


these models related problem:

models.py  class sequencediagram(models.model):     name = models.textfield(blank=true)     attributemappingname = models.textfield(blank=true)  class attributefilter(models.model):     seqdiagram = models.foreignkey(sequencediagram)     attributename = models.textfield(blank=true)     protocol = models.textfield()     isdisplayed = models.booleanfield(default=false)  class attributemapping(models.model):     mappingname = models.textfield()     protocol = models.textfield(blank=true)     nativename = models.textfield(blank=true)     customname = models.textfield(blank=true 

filters specific each sequencediagram mappings generic , applicable different diagrams.

i want formset attributefilters , attributemappings linked sequencediagram.

these displayed in table isdisplayed , customname can edited , saved database.

how can combine them formset , save users changes?

can many-to-many relationship solve problem? if so, in end should defined?

please tell me if needs clarified.

edit

the resulting table should this:

protocol|native|custom|display  prot1  | nat1 | cus1 | chkbx1  prot2  | nat2 | cus2 | chkbx2  ....... 

so matching customnames , isdisplayed aligned.

i have tried using objects.extra() can't seem save changes 'other' model, don't know how queryset formfield , back.

attributefilter.objects.extra( select={"protocol":"protocol", "sd":"sdattributename"}, where=["customname=nativename"], tables=["project_attributemapping"])

you can display both forms in template , process forms separately in view.

here, using modelforms. create modelform each model , save models in single view.

forms.py

from django import forms  my_app.models import sequencediagram, attributefilter, attributemapping  class sequencediagramform(forms.modelform):      class meta:         model = sequencediagram  class attributefilterform(forms.modelform):      class meta:         model = attributefilter         exclude = (seqdiagram,)  class attributemappingform(forms.modelform):      class meta:         model = attributemapping 

views.py

from django.views.generic import view  class myview(view):      def post(self, request, *args, **kwargs):         sequence_diagram_form = sequencediagramform(request.post) # create form instance , populate data         attribute_filter_form = attributefilterform(request.post) # create form instance , populate data         attribute_mapping_form = attributemappingform(request.post) # create form instance , populate data          sequence_diagram_form_valid = sequence_diagram_form.is_valid() # check if 'sequencediagramform' valid         attribute_filter_form_valid = attribute_filter_form.is_valid() # check if 'attributefilterform' valid         attribute_mapping_form_valid = attribute_mapping_form.is_valid() # check if 'attributemappingform' valid          # check if forms valid         if sequence_diagram_form_valid , attribute_filter_form_valid , attribute_mapping_form_valid:             sequence_diagram_obj = sequence_diagram_form.save() # save sequencediagram object             attribute_filter_obj = attribute_filter_form.save(commit=false) # not save instance              attribute_mapping_obj = attribute_mapping_form.save() # save attributemapping object             attribute_filter_obj.seqdiagram = sequence_diagram_obj  # set `seqdiagram` `sequence_diagram_obj`             attribute_filter_obj.save() # save attributefilter object             ...             # redirect success page on all forms being valid          ...             # render page again errors if of form invalid 

in our view, check if forms valid , save 3 objects db. if of form invalid, don't save of them.
case when of form invalid, can add code render page again form errors.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -