我的应用程序中有两个视图(每个视图都有一个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' ]
答案 0 :(得分:0)
您是否可以将Derogation.type_derogation
和Derogation.createur
拆分为单独的DerogationRaison
模型,并向ForeignKeyField
添加一个(可空的)Derogation
? (在不知道您的UI流程/用例的情况下很难回答)