2011-10-10 21 views
6

基本クラスのネストされたクラスメンバのいくつかを "オーバーライド"し、残りはそのまま維持する必要があります。
これは私が何をすべきかです:Pythonでネストされたクラスメンバーをオーバーライドするにはどうすればよいでしょうか?

実際に
class InternGenericForm(ModelForm):     
    class Meta: 
     model = Intern 
     exclude = ('last_achievement', 'program',) 
     widgets = { 
      'name': TextInput(attrs={'placeholder': 'Имя и фамилия' }), 
     } 

class InternApplicationForm(InternGenericForm): 
    class Meta: 
     # Boilerplate code that violates DRY 
     model = InternGenericForm.Meta.model 
     exclude = ('is_active',) + InternGenericForm.Meta.exclude 
     widgets = InternGenericForm.Meta.widgets 

、私はInternApplicationForm.Meta正確InternGenericForm.Metaのような、そのexcludeタプルが1つのより多くの項目を含まなければならないことを除いてになりたいです。

これをPythonで行うもっと美しい方法は何ですか?
model = InternGenericForm.Meta.modelのような定型コードを記述する必要はなく、エラーが発生しやすいと思います。

答えて

13
class InternGenericForm(ModelForm):     
    class Meta: 
     model = Intern 
     exclude = ('last_achievement', 'program',) 
     widgets = { 
      'name': TextInput(attrs={'placeholder': 'Имя и фамилия' }), 
     } 

class InternApplicationForm(InternGenericForm): 
    class Meta(InternGenericForm.Meta): 
     exclude = ('is_active',) + InternGenericForm.Meta.exclude 
関連する問題