Reputation: 360
I have the following form:
class MyForm(forms.Form):
class MyField(forms.CharField):
def __init__(self, *args, **kwargs):
kwargs.setdefault('max_length', 320)
super(self.__class__, self).__init__(*args, **kwargs)
sms_eng = MyField(label=_('Text for SMS'))
What bothers me is that I can not call super as I would usually do:
super(MyForm.MyField, self).__init__(*args, **kwargs)
Is defining fields inside forms recommended? Where else should I define fields that are used only in one form? What are the drawbacks of super(self.__class__, self)
as opposed to super(A.B, self)
?
Upvotes: 0
Views: 59
Reputation: 599510
Defining a class inside a class is very rarely useful in Python generally - there is nothing to be gained by doing so, and the inner class does not get any special access to the outer class as in say Java.
Just put the field definition before the form class and use it as normal.
Also, you must never use self.__class__
inside a super call. This always refers to the concrete class, not the class at that level in the hierarchy, so is likely to lead to an infinite loop if your class is ever subclassed in turn.
Upvotes: 2