2012-03-25 76 views
0

我有一個Web應用程序,它允許用戶創建一個帳戶,並在此過程中創建一個用戶對象,形成與自定義UserProfile模型關聯的標準Django用戶模型,以及地址模型。我已經構建了一個HTML表單,允許用戶通過使用AddressInfoForm和UserProfileForm的子類來更新他們的地址和配置文件;這兩者都是ModelForms,如下:保存具有多繼承性的Django模型表格

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,然後調用地址和用戶配置的保存功能如下:然而

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

,不爲自己的工作情況是用戶對象,因此上述兩種上述函數返回一個用戶對象

2)是我執行ContactInfoForm的初始化方法的預先填充HTML表單的最好方式,當用戶第一次訪問更新聯繫信息頁面?換句話說,params字典的構造是否正確,並將其用作初始參數。請記住,我有權訪問request.user中的用戶對象,因爲此視圖位於login_required裝飾器的後面...

3)是否有更好的方法可以實現我試圖實現的目標,複雜和更多的Django/Pythonic?

+0

'類ContactInfoForm(AddressForm,UserProfileForm):...'將不起作用,因爲ModelForms不允許多重繼承。請參閱https://code.djangoproject.com/ticket/7018 – 2012-03-26 14:18:29

回答

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() 

也許這是更多的代碼,但它更清晰易讀。