Monday, December 29, 2014

modelformset question - interspersing extra forms among existing forms


I have the following pseudo model that I will use as the basis of a modelformset:

class Entry:
    athlete = ForeignKey(athlete)
    category = ForeignKey(category)
    race_number = IntegerField

I have a page that displays each athlete's details so the choice of athlete has already been made. I want to add a formset that allows a user to specify a race_number for each of the possible categories available.

An obvious way would be for there to be one Entry for each athlete/category, but in practise this will result in a large entry table that is only ~10% actual data, with the rest being superfluous rows. I'd prefer to avoid this.

Let's assume that an athlete is already entered in 2 categories out of a possible 10. The queryset will return the 2 existing entries, and I can use extra=8 to add in the other possibilities. However, I am not yet done. I want to do the following and would like advice on how to accomplish this:
  1. The category for each the 8 possible new entries needs to be assigned before displaying the form - I already know what they are and want to display these categories to the user, leaving the race_number as the only input field.
  2. The categories have a particular (chronological) order that I want respected in displaying the forms to the user, so the 2 existing and 8 possible new categories need to be interspersed to follow the correct order.
  3. When it comes time to save the formset, I want only those categories that have been changed by having a race_number assigned or removed to be saved. I want the entries where the race_number has been removed to remain in the entry table. I think I need a way to detect and remove the forms that have not been changed by the user before saving the formset.

Any pointers as to how to do this?


You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment