Is it possible to supply a clean_[fieldname] method to a ModelForm for
custom validation rules like you can with a normal Form?  I've got a
form with an author column that maps to Django's User class.  I want a
default to be set if none is given.  I've tried many ways to
accomplish this and haven't found one...

I don't want to tie it to the model directly as going through
different logical paths means slightly different defaults.

I tried adding a `clean_author` method but that didn't seem to work
either.  Simple example (not the actual code):

    class ClipForm(ModelForm):
        class Meta:
            model = Clip
        def clean_author(self):
            if not self.cleaned_data.get('author', None):
                return User.objects.get(pk=1)

In my view, I have:

    if request.method == 'POST':
        form = ClipForm(data=request.POST)
        if form.is_valid():
            new_clip = form.save()
            # ...
        else:
            # ...

My understanding is that the is_valid() method kicks off the
Field.clean(), Field.clean_*(), and Form.clean() methods which should
call my clean_author method and assign an author if one isn't given.
But what happens is that is_valid() returns false with the error that
the author field is required.

Any guidance is appreciated.

Thanks,
Rob

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to