使用多重继承保存Django ModelForms

时间:2012-03-25 01:40:45

标签: python django inheritance modelform

我有一个允许用户创建帐户的Web应用程序,这样做可以创建一个标准Django User模型的用户对象,与自定义UserProfile模型相关联,以及一个Address模型。我已经构建了一个HTML表单,允许用户通过使用AddressInfoForm和UserProfileForm的子类来更新他们的地址和配置文件;两者都是ModelForms,如下所示:

class AddressForm(forms.ModelForm):
    class Meta:
        model = common_models.Address
        exclude = ('updated_dt','address_type','created_dt')

    def __init__(self,*args,**kwargs):
        super(AddressForm,self).__init__(*args,**kwargs)

    firstname = forms.CharField(max_length=100, min_length=1, error_messages={'required':'Please Enter First Name'})
    lastname = forms.CharField(max_length=100, min_length=1, error_messages={'required':'Please Enter Last Name'})
    address1 = forms.CharField(max_length=100, min_length=1, error_messages={'required':'Please Enter Address'})
    etc...

class UserProfileForm(forms.ModelForm): 
    class Meta:
        model = common_models.UserProfile
        exclude = ('created_dt','updated_dt','entity_active','profile_hash','user','address')

    account_type = forms.ChoiceField(choices=account_choices,widget=forms.Select(attrs={'id':'account_type_list'}),error_messages={'required':'Please Select Account Type'})
    name = forms.CharField(max_length=100, min_length=1, error_messages={'required':'Please Company Name'})
    supplier_type = forms.ModelChoiceField(queryset=common_models.SupplierTypeCode.objects.all(),required=False,widget=forms.Select(attrs={'id':'account_type_select'}))
    buyer_type = forms.ModelChoiceField(queryset=common_models.ClientTypeCode.objects.all(),widget=forms.Select(attrs={'id':'account_type_select'}),required=False)


class ContactInfoForm(AddressForm,UserProfileForm): 
    class Meta:
        model = common_models.User
        exclude = ('email','username',
           'password','last_login','date_joined')

    def __init__(self,user=None,request_post_data=None,*args,**kwargs):
        if not request_post_data:
                params = dict([tuple([k,v]) for k,v in user.get_profile().address.__dict__.items()] +
            [tuple([k,v]) for k,v in user.get_profile().__dict__.items()])
                super(ContactInfoForm,self).__init__(initial=params,*args,**kwargs)
            else:
                super(ContactInfoForm,self).__init__(request_post_data,instance=user)

现在,我有以下问题:

1)如何保存ContactInfoForm,以便更新user_profile和地址表以及auth_user表?我试过覆盖ContactInfoForm中的save函数,然后调用Address和UserProfile的save函数,如下所示:

def save(self):
    address = AddressForm.save(self)
    profile = UserProfileForm.save(self)

然而,这不起作用,因为self的实例是用户对象,因此上述两个函数都返回一个用户对象

2)当用户首次访问更新联系信息页面时,我对ContactInfoForm的 init 方法的实现是预先填充HTML表单的最佳方式吗?换句话说,是params字典的构造并使用它作为正确的初始参数。请记住,我可以从request.user访问用户对象,因为此视图位于login_required装饰器后面...

3)是否有更好的方法来实现我想要实现的目标,而不是那么复杂和更多的Django / Pythonic?

1 个答案:

答案 0 :(得分:2)

通常在Django中,这样的事情是通过创建3个单独的表单并在一个视图中处理它们来完成的。

address_form = AddressForm(request.POST)
profile_form = UserProfileForm(request.POST)
contacts_form = ContactInfoForm(request.POST)
if address_form.is_valid() and profile_form.is_valid() and contacts_form.is_valid():
    address_form.save()
    profile_form.save()
    contacts_form.save()

也许这样的代码更多,但它更清晰易读。