On Wed, Jan 6, 2010 at 11:13 AM, Aristotle Miternan
<hypersp...@gmail.com> wrote:
> Hello Karen,
>
> Here is a minimal example of what I am trying to do. I am trying to
> have a save/load functionality before a user posts their information.
> This gives me an UnresolvableValidationError because the id of the
> model is undefined when I call is_valid(). I tried a workaround by
> doing: def full_validation(): pass  in my model, but now the modelform
> is not saving the data to the form (although all the cleaned_data is
> there).
>
> class Foo(models.Model):
>    creator = models.ForeignKey(User)
>    created = models.DateTimeField('date created')
>    title = models.CharField( max_length=80 )
>
>
> class FooForm(forms.ModelForm):
>        class Meta:
>                model = Foo
>                fields = ('title',)
>
> def save(request):
>    """Called when a submit button is clicked, not used for GET
> requests."""
>    def bind_instance(fooform, foo_pk):
>        try:
>                db_foo = Foo.objects.get(pk=foo_pk)
>                fooform.just_created = False
>        except Foo.DoesNotExist:
>                db_foo = Foo()
>                fooform.just_created = True
>        fooform.instance = db_foo
>        return fooform
>
>    if request.method == "GET":
>          return ERROR_CONDITION
>
>    myform = FooForm( request.POST )
>    if myform.is_valid():
>          primary_key = request.session['editing_foo'] # -1 if new,
> else returns the key of the foo being edited
>          fooform = bind_instance(fooform, primary_key)
>          foo = fooform.save(commit=False)
>
>          # Title should be handled by modelform
>          if fooform.just_created:
>               foo.creator = request.user
>               foo.created = datetime.datetime.now()
>         foo.save()
>         return render_to_response( 'success.html', {},
> context_instance=RequestContext(request) )
>
>    else:
>         return render_to_reponse( 'sometemplate.html', { 'form' :
> myform }, context_instance=RequestContext(request) )
>
> Thanks!

There's no bug here. It's also arguable whether you have been bitten
by a backwards incompatible change - you're relying on manipulating
the internals of a ModelForm after it has been created, and the
internals of a class aren't ever guaranteed to be backwards
compatible.

It looks to me like you're making your life a lot more complicated
than it needs to be. Here's a cleaned up version of the same view:

def save(request):
    if request.method == "GET":
        return ERROR_CONDITION

    try:
        pk = request.session['editing_foo']
        db_foo = Foo.objects.get(pk=pk)
    except Foo.DoesNotExist:
        db_foo = Foo()
        db_foo.creator = request.user
        db_foo.created = datetime.datetime.now()

    myform = FooForm(instance=db_foo, data=request.POST)
    if myform.is_valid():
        my_form.save()
        return render_to_response('success.html',
            {}, context_instance=RequestContext(request))

   else:
        return render_to_reponse('sometemplate.html',
            { 'form' :myform },
            context_instance=RequestContext(request)
        )

This is the same view, but with the ModelForm usage corrected to use
the documented API.

Completely independent of this change, you should probably change the
'success' render_to_response to a HttpResponseRedirect - otherwise
pressing reload on the success page will cause the form to be
resubmitted and reprocessed.

Yours,
Russ Magee %-)
-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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