如何在两个视图中创建两个窗体填充不同字段的对象?

时间:2019-06-14 14:18:20

标签: django django-forms

我的应用程序中有两个视图(每个视图都有一个ModelForm),我想用两个表单返回的值创建一个对象。我该如何进行?

views.py:

def index(request):
    derog_indiv_form = DerogationIndividuForm(request.POST or None)
    if derog_indiv_form.is_valid():
        # Some code
    return render(request, 'derog_bv/index.html', {'derog_indiv_form':derog_indiv_form})

def raison(request):
    derog_raison_form = DerogationRaisonForm(request.POST or None)
    if derog_raison_form.is_valid():
        # Some code
    return render(request, 'derog_bv/raison.html', {'derog_raison_form':derog_raison_form})

forms.py:

class DerogationIndividuForm(ModelForm):
    class Meta:
        model = Derogation
        fields = [ 'individu' ]
        labels = { 'individu': ('Individu :') }

class DerogationRaisonForm(ModelForm):
    class Meta:
        model = Derogation
        fields = [ 'type_derogation' , 'createur' ]

1 个答案:

答案 0 :(得分:0)

您是否可以将Derogation.type_derogationDerogation.createur拆分为单独的DerogationRaison模型,并向ForeignKeyField添加一个(可空的)Derogation? (在不知道您的UI流程/用例的情况下很难回答)