On Fri, 2009-03-06 at 12:25 -0800, mike171562 wrote:
> I am using Django's ModelForm in a template im working on. I am trying
> to use it to display the Django User Object and an extension I added
> of the user model, what would be the correct way to display my User
> model extension in my template, I would like to be able to edit the
> User information and the account number I have added all from the same
> form. Thanks.

A Django Form object represents only a part of an HTML form (which is
why need to write the HTML "form" tag in the template, for example). You
can pass multiple form objects through from your view function to the
template and render both of them inside the same HTML form.

If you want to intermingle fields, you can define extra form fields on a
model form. For example:

        class MyForm(forms.ModelForm):
            extra = forms.CharField(max_length=50)
        
            class Meta:
                model = models.Tag
        

That will put the extra fields at the end of the model form.

If you want to put the extra fields in the middle of the model fields
somehow, you can either write a custom __init__ method to tweak the
field ordering (you'll have to read the forms code a bit to see what
needs tweaking). Alternatively, you can take the not unreasonable
approach that you've gone beyond the scope of modelforms by this point
and just write a normal Form class that contains the fields.

I've always been an interested observer of people trying to make
automatic model-related forms do all sorts of funky things, because I
hardly ever use them. So many of my use-cases don't have forms mapping
directly to models, so I write a normal Form class and then my view
knows how to take fields from the Form instance and convert that data to
the appropriate models. At some point, it's a fairly grief-free way to
handle anything beyond the straight model -> form -> model conversion.
But, as you can see from the above, you aren't short of options here.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to