On Sat, Oct 3, 2009 at 2:55 AM, Tiago Samahá <tiagosam...@gmail.com> wrote:
>
> Hello All,
>
> i'm trying generate a single form with two models.
>
> class Contact(models.Model):
>    phone = models.CharField(max_length=8)
>    email = models.CharField(max_length=50)
>
> class Client(models.Model):
>    name = models.CharField(max_length=50)
>    type = models.CharField(max_length=10)
>    contact = models.OneToOneField("Contact")
>
> How can i generate a single form to insert a new client? What can i
> use? ModelForm, Formsets?

Well... if you want a single form, define a single form:

class MyForm(Form):
    phone = fields.CharField(max_length=8)
    email = fields.CharField(max_length=50)
    name = fields.CharField(max_length=50)
    ...

If you want to automatically generate a single form, you're out of
luck. A ModelForm operates on a single model class. You can't build a
model class for two models.

However, this isn't a major problem. There's no requirement that you
use a single form in a view. You can easily use multiple forms in a
single view - for example:

def edit_client(request, pk):
    client = get_object_or_404(Client, pk=pk)
    if request.method == "POST":
        contact_form = ContactForm(instance=client.contact, data=request.POST)
        client_form =ClientForm(instance=client, data=request.POST)
        if client_form.is_valid() and contact_form.is_valid():
            client_form.save()
            contact_form.save()
            return HttpResponseRedirect(...)
    else:
        contact_form = ContactForm(instance=client.contact)
        client_form =ClientForm(instance=client)
    return render_to_response(...)

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
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