So subclassing the generated form class does work, just type something
like:

class MyForm(forms.form_for_model(MyModel)):
 my_field = forms.CharField()
 ...

(Python is very interesting :)

But there's a not-so-small catch: new fields defined in the subclass
don't get added to the fields dictionary. It's easy to see why when
looking at the code for form_for_model() and newforms.Form. Basically
the fancy metaclass stuff that turns class fields into form fields
isn't done when using form_for_model(), and form_for_model() only looks
of the models fields.

There are probably a few ways around this. One would be to
programatically add fields to either the class returned by
form_for_model() or the instance in question. This is probably the
easiest, but least elegant way to do what you want.

Another might be to create a metaclass similar to
DeclarativeFieldsMetaclass that adds your class fields to the fields
list already generated by form_for_model(). I'm not sure how to do this
yet, but I'll try to figure it out.

Yet another way would be to write your own version of form_for_model()
that appends the models fields to the fields of the form class passed
in. Then your form class isn't a subclass of the generated class, but a
standalone class inheriting from newforms.Form. You would call it like:

my_form_class = my_form_for_model(MyModel, form=MyForm)


I'll play around with this some more later.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to