Struggling with formsets

24 views
Skip to first unread message

David

unread,
May 18, 2012, 9:27:05 AM5/18/12
to django...@googlegroups.com
Given 2 models:

class Person(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)

class PersonAttendance(models.Model):
    person = models.ForeignKey(Person)
    attend_date = models.DateField()

I need to produce a formset that lists all Person(s) and their PersonAttendance if it has been entered, and blank fields for Persons that have no PersonAttendance related record so their attendance can be filled in.

In SQL I guess this would be achieved using a LEFT JOIN on Person, but I can't work out how to get this working with the ORM and Django Formsets.

Any help would be greatly appreciated, I've been stumped on this for ages.

Thank you

pedesen

unread,
May 18, 2012, 10:12:13 AM5/18/12
to django...@googlegroups.com
For this case inline-formsets should be the way to go: https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#inline-formsets
But I think this will work for a particular person, but not for multiple persons...

David

unread,
May 18, 2012, 10:20:20 AM5/18/12
to django...@googlegroups.com
Thanks Pedesen for your reply.

I have tried inline-formsets and as you say unfortunately I could only get it to work with a single object.

David

unread,
May 29, 2012, 5:45:58 AM5/29/12
to django...@googlegroups.com
Anyone else have any suggestions please?

Thank you

akaariai

unread,
May 29, 2012, 6:09:34 AM5/29/12
to Django users
On May 29, 12:45 pm, David <davidwi...@adviserbreakthrough.co.uk>
wrote:
> Anyone else have any suggestions please?
>
> Thank you

I don't think there is a way to create formsets, and then formsets
related to the first formsets. So, something like this is what you
likely need:

for p in persons:
do_what_you_did_for_single_object() # remember to prefix the
forms.

I would likely do a list persons + their attendances view with edit
link/button for each person. That is one approach worth considering,
as it is pretty easy to do.

- Anssi
Reply all
Reply to author
Forward
0 new messages