I am working on a Django application but this seems like it is just a python question, with nothing necessarily specific to Django. I'm pretty new to python, and its hard to describe what I am trying to do, but easier to show so here goes:
I have one class:
class SlideForm(ModelForm):
class Meta:
model = Slide
which I subclass:
class HiddenSlideForm(SlideForm):
def __init__(self, *args, **kwargs):
super(HiddenSlideForm, self).__init__(*args, **kwargs)
for name, field in self.fields.iteritems():
field.widget = field.hidden_widget()
field.required = False
and then I have another class:
class DeckForm(ModelForm):
def __init__(self, *args, **kwargs):
# do some stuff here
return super(DeckForm, self).__init__(*args, **kwargs)
class Meta:
model = Deck
# other stuff here
which I also sub-class:
class HiddenDeckForm(DeckForm):
def __init__(self, *args, **kwargs):
super(HiddenDeckForm, self).__init__(*args, **kwargs)
for name, field in self.fields.iteritems():
field.widget = field.hidden_widget()
field.required = False
Note that the subclasses have the exact same code other than class names and do the exact same thing. I have been trying to figure what the best way to genericize this so I can keep it DRY and easily use it for other classes, and have considered decorators and/or multiple inheritance--both of which are new concepts for me--but I keep getting mixed up.
Help is appreciated!
(As a side note, feel free to point out any problems you see in my django code :) )