在Formset中组合不相关的模型并保存结果

时间:2015-07-14 14:55:19

标签: django django-models django-forms

这些是与我的问题相关的模型:

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

过滤器特定于每个SequenceDiagram,但映射是通用的,适用于不同的图表。

我想要一个Formset,其中所有AttributeFilters和AttributeMappings都与SequenceDiagram相关联。

这些将显示在isDisplayed表中,并且可以编辑customName,然后保存到数据库中。

  

如何将它们组合到Formset然后保存用户更改?

多对多关系可以帮助解决我的问题吗?如果是这样,应该在哪个定义?

请告诉我是否需要澄清。

修改

结果表应如下所示:

Protocol|Native|Custom|Display
 prot1  | Nat1 | Cus1 | Chkbx1
 prot2  | Nat2 | Cus2 | Chkbx2
 .......

因此匹配的customNames和isDisplayed是对齐的。

我尝试过使用objects.extra(),但我似乎无法将更改保存到“其他”模型,我也不知道如何将查询集保存到Formfield并返回。

AttributeFilter.objects.extra( select={"protocol":"protocol", "sd":"sdAttributeName"}, where=["customName=nativeName"], tables=["project_attributemapping"])

1 个答案:

答案 0 :(得分:0)

您可以在模板中显示这两个表单,并在视图中单独处理表单。

在这里,我们将使用ModelForms。我们将为每个模型创建ModelForm,然后将所有模型保存在单个视图中。

forms.py

from django import forms 
from 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 and populate with data
        attribute_filter_form = AttributeFilterForm(request.POST) # create form instance and populate with data
        attribute_mapping_form = AttributeMappingForm(request.POST) # create form instance and populate with data

        sequence_diagram_form_valid = sequence_diagram_form.is_valid() # check if 'SequenceDiagramForm' is valid
        attribute_filter_form_valid = attribute_filter_form.is_valid() # check if 'AttributeFilterForm' is valid
        attribute_mapping_form_valid = attribute_mapping_form.is_valid() # check if 'AttributeMappingForm' is valid

        # Check if all the forms are valid
        if sequence_diagram_form_valid and attribute_filter_form_valid and attribute_mapping_form_valid:
            sequence_diagram_obj = sequence_diagram_form.save() # save the SequenceDiagram object
            attribute_filter_obj = attribute_filter_form.save(commit=False) # not save but get the instance 
            attribute_mapping_obj = attribute_mapping_form.save() # save the AttributeMapping object
            attribute_filter_obj.seqDiagram = sequence_diagram_obj  # set the `seqDiagram` to `sequence_diagram_obj`
            attribute_filter_obj.save() # Now save the AttributeFilter object
            ...
            # redirect to success page on all all forms being valid

        ...    
        # render the page again with errors if any of the form is invalid

在我们的视图中,我们检查所有表单是否有效,然后只将所有3个对象保存到db中。如果任何表格无效,则我们不保存任何表格 对于任何表单无效的情况,您可以添加代码以使用表单错误再次呈现页面。