猿问

单个Django ModelForm中有多个模型?

ModelFormDjango 是否可以在一个模型中包含多个模型?我正在尝试创建个人资料编辑表单。因此,我需要包括User模型和 UserProfile模型中的某些字段。目前我正在使用2种形式


class UserEditForm(ModelForm):


    class Meta:

        model = User

        fields = ("first_name", "last_name")


class UserProfileForm(ModelForm):


    class Meta:

        model = UserProfile

        fields = ("middle_name", "home_phone", "work_phone", "cell_phone")

有没有一种方法可以将这些合并为一个表单,或者我是否只需要创建一个表单并处理数据库加载并保存自己?


胡子哥哥
浏览 1122回答 3
3回答

明月笑刀无情

您可以只在一个<form>html元素的模板中显示两种形式。然后,只需在视图中分别处理表单即可。您仍然可以使用form.save()而不需要处理数据库加载和保存您自己。在这种情况下,您不需要它,但是如果您要使用具有相同字段名的表单,请查看prefixdjango表单的kwarg。

江户川乱折腾

我和erikbwork都有一个问题,即一个模型只能包含在一个通用的基于类的视图中。我找到了类似苗的类似方法,但是更加模块化。我写了一个Mixin,因此您可以使用所有通用的基于类的视图。定义模型,字段,现在还定义child_model和child_field-然后可以将两个模型的字段包装在标签中,如Zach描述。class ChildModelFormMixin:&nbsp;&nbsp; &nbsp; ''' extends ModelFormMixin with the ability to include ChildModelForm '''&nbsp; &nbsp; child_model = ""&nbsp; &nbsp; child_fields = ()&nbsp; &nbsp; child_form_class = None&nbsp; &nbsp; def get_child_model(self):&nbsp; &nbsp; &nbsp; &nbsp; return self.child_model&nbsp; &nbsp; def get_child_fields(self):&nbsp; &nbsp; &nbsp; &nbsp; return self.child_fields&nbsp; &nbsp; def get_child_form(self):&nbsp; &nbsp; &nbsp; &nbsp; if not self.child_form_class:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.child_form_class = model_forms.modelform_factory(self.get_child_model(), fields=self.get_child_fields())&nbsp; &nbsp; &nbsp; &nbsp; return self.child_form_class(**self.get_form_kwargs())&nbsp; &nbsp; def get_context_data(self, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; if 'child_form' not in kwargs:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kwargs['child_form'] = self.get_child_form()&nbsp; &nbsp; &nbsp; &nbsp; return super().get_context_data(**kwargs)&nbsp; &nbsp; def post(self, request, *args, **kwargs):&nbsp; &nbsp; &nbsp; &nbsp; form = self.get_form()&nbsp; &nbsp; &nbsp; &nbsp; child_form = self.get_child_form()&nbsp; &nbsp; &nbsp; &nbsp; # check if both forms are valid&nbsp; &nbsp; &nbsp; &nbsp; form_valid = form.is_valid()&nbsp; &nbsp; &nbsp; &nbsp; child_form_valid = child_form.is_valid()&nbsp; &nbsp; &nbsp; &nbsp; if form_valid and child_form_valid:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self.form_valid(form, child_form)&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return self.form_invalid(form)&nbsp; &nbsp; def form_valid(self, form, child_form):&nbsp; &nbsp; &nbsp; &nbsp; self.object = form.save()&nbsp; &nbsp; &nbsp; &nbsp; save_child_form = child_form.save(commit=False)&nbsp; &nbsp; &nbsp; &nbsp; save_child_form.course_key = self.object&nbsp; &nbsp; &nbsp; &nbsp; save_child_form.save()&nbsp; &nbsp; &nbsp; &nbsp; return HttpResponseRedirect(self.get_success_url())用法示例:class ConsumerRegistrationUpdateView(UpdateView):&nbsp; &nbsp; model = Registration&nbsp; &nbsp; fields = ('firstname', 'lastname',)&nbsp; &nbsp; child_model = ConsumerProfile&nbsp; &nbsp; child_fields = ('payment_token', 'cart',)或使用ModelFormClass:class ConsumerRegistrationUpdateView(UpdateView):&nbsp; &nbsp; model = Registration&nbsp; &nbsp; fields = ('firstname', 'lastname',)&nbsp; &nbsp; child_model = ConsumerProfile&nbsp; &nbsp; child_form_class = ConsumerProfileForm做完了 希望能对某人有所帮助。
随时随地看视频慕课网APP
我要回答