Hi,

I'm a bit stuck with inline formset. I missed something.
I'm using django 1.1.1.

I have a model:

class Address(models.Model):
    street = ...
    number = ...
    ...

class Person(models.Model):
    first_name = ...
    last_name =
    address = models.ForeignKey(Address)
    ...

Based on this model, I would like to have one form to add a person and her 
address. In order to achieve this, I'm using inline formset.

As far as I understood, formset allows to add multiple form on a page and 
inline formset is an extension that allows to play with related objects.

I written the following view (based on the documentation located at [1])

[1] http://docs.djangoproject.com/en/1.1/topics/forms/modelforms/#inline-
formsets

def add_person_form(request):

    address = Address()
    PersonFormSet = inlineformset_factory(Address, Person, max_num = 1, 
can_delete = False)

    if request.method == 'POST':
        formset = PersonFormSet(request.POST, request.FILES, instance = 
address)
        if formset.is_valid():
            formset.save()
            return redirect('/person')
    else:
        formset = PersonFormSet(instance = address)
    return render_to_response('person/add_form.html', { "formset": formset } )


My template looks like (simplified version):

<form action="" method="post">
     {{ formset }}
</form>

This only displays the person but not the address.

Extra question: if I want to display the fields in a given order, let's say: 
first_name, last_name, and then all fields from Address object followed by the 
remaining fields from Person. What is the best way of doing it ?

Thanks for your support,

Frédéric Burlet.

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to