Hi,

Your case is complicated enough that I'd actually recommend not using 
formsets here and processing a list of plain ModelForms yourself in the 
view. I bet formsets will ending out getting in your way.

Here's a some messy code:

class EntryForm(forms.ModelForm):
    race_number = Entry._meta.get_field('race_number').formfield(required=
False)
    class Meta:
        model = Entry
        fields = ['race_number']

def the_view(request, athlete_id):
    athlete = get_object_or_404(Athlete, id=athlete_id)
    existing = {e.category_id: e for e in athlete.entry_set.all()}
    entries = []
    for category in Category.objects.order_by('particular'):
        entries.append(existing.get(category.pk, Entry(athlete=athlete, 
category=category)))
    if request.method = 'POST':
        forms = [EntryForm(request.POST, instance=e, prefix=e.category.pk) 
for e in entries]
        if all([f.is_valid() for f in forms]):  # be sure to call 
is_valid() on every form
            for entry in entries:
                if entry.race_number:
                    entry.save()
                if entry.pk and not entry.race_number:
                    entry.delete()
    else:
        forms = [EntryForm(instance=e, prefix=e.category.pk) for e in 
entries]
    return render(request, 'template.html', {'athlete': athlete, 'forms': 
forms})

{% for form in forms %}
Category: {{ form.instance.category }}
{{ form }}
{% endfor %}

Collin

On Monday, December 29, 2014 6:07:19 PM UTC-5, Richard Brockie wrote:
>
> Hi,
>
> 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?
>
> Thanks!
> R.
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/522acc71-fa69-4b2d-8b6d-b918a072eaf2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to