2012-11-21 21 views
1

我有一個繼承自Action的類InheritAction。現在我想要做的是爲PositiveIntegerField提供不同的選擇。我知道我們不能覆蓋django中的類屬性。但是有沒有辦法做到這一點。在此先感謝Django:提供用於繼承類的PositiveIntegerField的自定義選擇

 ACTION_TYPE = (
      (1, 'Approve'), 
      (2, 'Reject'), 
      (3, 'More Information Required'), 
      (4, 'Status Update') 
      ) 
    class Action(models.Model): 
     type = models.PositiveIntegerField(choices=ACTION_TYPE) 

    INHERIT_ACTION_TYPE = (
      (1, 'Approve'), 
      (2, 'Reject'), 
      (3, 'More Information Required'), 
      (4, 'Status Update') 
      ) 
    class InheritAction(Action): 
     pass 

我曾嘗試這樣做...

InheritAction._meta.get_field('type).choices = INHERIT_ACTION_TYPE 

但給出了一個錯誤......

AttributeError: can't set attribute 

回答

0

您可以覆蓋形式的選擇。

class InheritActionForm(forms.ModelForm): 
    type = forms.ChoiceField(choices = INHERIT_ACTION_TYPE) 

    class Meta: 
     model = InheritAction 

你可以use that form in the admin如果你需要它

據我知道的選擇選項不加任何約束的數據庫,它只是自動生成的表單。