Hello again Collin,

I am using your example as the basis for another similar set of forms and I 
spotted something subtle that passed me by the first time through.

Namely, that to save the data, you are calling the model.save() method, 
rather than the form.save() method that is generally shown in the 
documentation examples.

I've tried both, and they both work. From the documentation I was expecting 
that this line:

    forms = [EntryForm(request.POST, instance=e, prefix=e.category.pk) for 
e in entries]

would be one-way, in that the POST and instances would combine in the forms 
and form.save() would be the method that should be used. It appears however 
that the POST data also end up in the instances.

I wasn't expecting that, but it appears to work. Is there a preferred or 
standard method that I should think about using?

R.

On Sunday, January 4, 2015 at 11:41:36 AM UTC-8, Collin Anderson wrote:
>
> Hi,
>
> If the field on the model is already blank=True, then you don't need that.
>
> Also, I realized my (completely untested :) code doesn't exactly match the 
> behavior you want, but I hope it's a good enough start. You may need to 
> store a reference to the original race_number to decide if you need to 
> .save() or not.
>
> Collin
>
> On Friday, January 2, 2015 9:42:47 PM UTC-5, Richard Brockie wrote:
>>
>> Hi Collin,
>>
>> Thanks very much for the advice. I'll be getting to this part of my 
>> development in the next few days and will report back when I have things 
>> working.
>>
>> One question in the meantime - what does this line do given that my model 
>> will already explicitly not require the race_number?
>>
>> race_number = Entry._meta.get_field('race_number').formfield(required=
>> False)
>>
>> Wishing everyone a prosperous New Year.
>> R.
>>
>>
>> On Thursday, January 1, 2015 3:31:45 PM UTC-8, Collin Anderson wrote:
>>>
>>> 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/0f5e6fb7-872d-45f5-bfea-0795ff39328f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to