On 23 Kwi, 11:17, Ryan  Kanno <[EMAIL PROTECTED]> wrote:
> I've been playing around with newforms (with a custom form) and I was
> just wondering if anyone else has run into the same situation and I'm
> curious as to what solutions others have come up with.  Basically, I
> just want to edit an instance and I've been consistently writing the
> following piece of code in my edit views.
>
> if request.method == 'POST':
>      # Fill in custom form with request post data
>      form = CustomForm(data = request.POST.copy())
>      if form.is_valid():
>           instance = get_object_or_404(InstanceClass, pk =
> request["object_id"])
>           for key in form.clean_data:
>                instance.__setattr__(key, form.clean_data[key])
>           instance.save()
>           return HttpResponseRedirect(instance.get_absolute_url())
>
> Fairly straightforward, if it's a POST, fill in the custom form with
> the data.  If the data validates, retrieve the instance object the
> form refers to, and overwrite data from the form into the instance,
> then update.
>
> I'm just wondering if this what others have been doing?  Seems cludgy
> to me. :)

Why don't you use forms.form_for_instance() ?
You can do many things with it (thanks to formfield_callback
parameter).

Your example would be good if no many2many data was used.

Take a look at:
http://code.djangoproject.com/browser/django/trunk/django/newforms/models.py

how it's done in forms_for_model/instance, save_instance,
save_instance, model_save or wait for complete documentation :)

--
Robert


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

Reply via email to