Obligatory counterpoint: it's not "intuitive", but that's an awfully
loaded word, since no two programmers ever born will agree on what is
and isn't "intuitive". And I'm not sure "intuitive" is even a good
thing to aim for, because...
A better counterpoint would be to consider this class:
class MyForm(forms.Form):
name = forms.CharField(max_length=25)
favorite_color = forms.CharField(max_length=10)
A simple form with two fields, right?
OK, how about this:
class MyOtherForm(MyForm):
favorite_food = forms.CharField(max_length=20)
By the same argument, this is "unintuitive" because you have to notice
that it's subclassing another form and adding a field, so that it has
three fields instead of just the one that's explicitly defined. And,
in fact, this sort of thing is not uncommon at all in Python; any time
you see something being subclassed, it's a sign you should find out
what the parent class does, because the subclass definition may not
tell the whole story.
In the case of a ModelForm, it's even a bit easier because you know --
from the inner 'Meta' class -- what model it's going to draw its base
field definitions from.
So I'm not convinced that there's any problem here that needs fixing;
you can't just skim over a piece of code without paying attention to
what it's doing (rather than being "intuitive", I like to call that
"dangerous"), so you see that it's subclassing ModelForm and either go
look up what that means (if you don't know what the ModelForm class
does) or look up the model it draws its fields from (if you do), and
there are no surprises.
--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."
Not really. I first notice it's a ModelForm subclass because I start
reading from the first line, not the second one. I remember that a
ModelForm class means the fields are initially derived from the model.
The same as "class Foo(Bar):..." tells me that there's behaviour coming
from the Bar class and the Foo class doesn't tell me everything.
> however maybe is a form with five fields. What is happening? You
> explicity define two fields, and the real action is a _redefinition_
> of two fields.
Yes, just as with every other case of subclassing.
>
> Django philosophy talk about "Explicit better than implicit", but in
> this point ModelForms is not very intuitive.
You're really arguing against Python (and most other languages')
subclassing behaviour. I don't find that particularly supportable.
IT sounds like you want a big sign that says "this isn't a normal Form".
We have that sign... it's spelt "ModelForm". A different type of sign
would be somewhat repetitive and non-Python at this point.
If you have an alternative proposal, pitch it, but your arguments so far
are really saying "I don't like Python's subclassing", so it's a bit
hard to go further on generalisations.
Regards,
Malcolm
--
Tolkien is hobbit-forming.
http://www.pointy-stick.com/blog/