On Mon, Nov 2, 2009 at 5:28 PM, Nick Arnett <nick.arn...@gmail.com> wrote:

>
>
> On Mon, Nov 2, 2009 at 3:30 AM, Julien Petitperrin <
> julien.petitper...@gmail.com> wrote:
>
>> Hello,
>>
>> I had this kind of issue a few days ago. I found this:
>>
>>
>> http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/
>>
>> I hope this helps you to find a way to do your multiple items edit form.
>
>
>
> Ah, that does look helpful and I wondered it I might do better to create
> one form per line, as it suggests.
>
> Nick
>

I am happy to help,

You should create one form per item and choose a smart prefix for each form
created, this example show only how to add multiple items.
But you can edit items too by using id as a prefix you will be able to get
an instance of the object for form creation.

something like that:

    form_submit = False
    if request.POST:
        # POST data => we should try to validate
        # always keep a list of ids of the item you edit
        ids_string = request.POST.get('ids', None)
        ids = ids_string.split(',')
        form_submit = True
        # we have post DATA we should validate/submit change
        # MyModelForm is a ModelForm Class
        all_forms = [MyModelForm(request.POST, prefix=str(id),
instance=model.objects.get(pk=id)) for id in ids]
        if all( [ form.is_valid() for form in all_forms] ):
            # All forms are valid we can update and save the object
            for form in all_forms:
                # update object field
                form.save()
            Message( id=None, user=request.user, message=_(u"success")
).save()
            return ttpResponseRedirect(
"Where/you/want/after/a/valid/edit/")
        else:
            Message( id=None, user=request.user, message=_(u"fail") ).save()

    if not form_submit:
        # no POST data we should initiate the forms
        # handle ids
        ids_string = request.GET.get('ids', None)
        ids = ids_string.split(',')
        # Create the Custom ModelForm
        all_forms = [ MyModelForm(prefix=str(id),
instance=model.objects.get(pk=id)) for id in ids ]

Hope this still helps

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to