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!
A.M.


On Jan 5, 9:02 pm, Karen Tracey <[email protected]> wrote:
> On Tue, Jan 5, 2010 at 8:41 PM, Aristotle Miternan 
> <[email protected]>wrote:
>
> > Oops, taking a second look, I think you are right, the object I was
> > using to test that had its __unicode__ method calling something that
> > shouldn't exist yet. I guess this example was not complex enough.
>
> > However, I think that I have figured out the problem. Model validation
> > was introduced in the latest patch, but I don't really understand how
> > it is different from what was before (not much explanation in the
> > docs). However, it seems that in ModelForms, clean() calls
> > self.instance.full_validate(). The problem with this is that I don't
> > necessarily have a >saved< instance assigned to the model yet. I don't
> > have enough data to save it before the form is evaluated! It seems
> > that I can work around this behavior by writing an overloaded
> > full_validate on my model, but this sounds flakey to me. Any thoughts?
>
> It would really help if you could show a minimal example of code that
> demonstrates the problem.
>
> Karen
-- 
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