2014-07-21 122 views
0

我正在用Django創建一個表單。此表單的ModelForm基於從基礎模型繼承的多個模型。該模型的結構與此類似:Django模型繼承了不同的模型,每個模型繼承自一個類

class BaseModel(models.Model): 
    first_name = models.CharField("First name", max_length=20) 
    middle_name = models.CharField("Middle name", max_length=20) 
    last_name = models.CharField("Last Name", max_length=20) 
    email = models.EmailField("Email address") 
    phone = models.CharField("Phone number", max_length=16) 

是由

class EmployerModel(BaseModel): 
    company = models.CharField("Company", max_length=20) 

和..

class AdvisorModel(BaseModel): 
    department = models.CharField("Department", max_length=20) 

它包含在我的最高水平模型(即模型繼承用在我的ModelForm中):

class FormModel(EmployerModel, AdvisorModel): 
    another_field = models.CharField(max_length=20) 
    and_another_field = models.CharField(max_length=20) 

#... 

class FormModelForm(forms.ModelForm): 

    class Meta: 
     model = FormModel 

我可以在製作表單時採用這種方法並避免ORM錯誤,因爲我有重複的字段名稱?有沒有辦法分開說話; 「這些領域適合'僱主';這些領域適合'顧問'?」

編輯

它看起來像我需要去與抽象基類,但我不知道是否能解決多重繼承問題。

回答

1

轉到抽象與父母的模型,我已經成功地寫型號,這種定義:使用Content作爲模型做工精細

class Content(ModeratedModel, NullableGenericModel, RatedModel, PicturableModel, PrivacyModel, CommentableModel): 
    pass 

ModelForm秒。