Re: clean_* method for ModelForms?
On Sun, 2008-07-06 at 13:44 -0700, Rob Hudson wrote: > So if I understand correctly, you are saying make author a > not-required field so is_valid() will validate to True. Then in my > view do the commit=False on save, check if author is present and if > not, set the default author, then call save(). Something like that? That's probably slight overkill, in that you don't need to go as far as getting back to the view and calling save(commit=False). You got the cleaning process exactly right the first time. Looks like the only thing that is going "wrong" (it's going right, actually -- doing exactly what you specified) is that the author field is required by the model and hence by the ModelForm. Since the field's validation includes checking "required-ness" and, if that fails, clean_*() isn't called, you're seeing the problem. You can set self.field["author"].required = False in your form's __init__ method to avoid this. Regards, Malcolm --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: clean_* method for ModelForms?
You don't have to do the commit=False thing. You can check if author is present in the clean() method, that will always be called. On Sun, Jul 6, 2008 at 10:44 PM, Rob Hudson <[EMAIL PROTECTED]> wrote: > > So if I understand correctly, you are saying make author a > not-required field so is_valid() will validate to True. Then in my > view do the commit=False on save, check if author is present and if > not, set the default author, then call save(). Something like that? > > On Sun, Jul 6, 2008 at 11:35 AM, Alex Koshelev <[EMAIL PROTECTED]> wrote: > > > > Oh, `required=True` of course > > > > On Jul 6, 10:31 pm, Alex Koshelev <[EMAIL PROTECTED]> wrote: > >> Field for `author` created with `required=False`. And `clean` method > >> doesn't call `clean_author` if `author` data isn't present. > > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: clean_* method for ModelForms?
So if I understand correctly, you are saying make author a not-required field so is_valid() will validate to True. Then in my view do the commit=False on save, check if author is present and if not, set the default author, then call save(). Something like that? On Sun, Jul 6, 2008 at 11:35 AM, Alex Koshelev <[EMAIL PROTECTED]> wrote: > > Oh, `required=True` of course > > On Jul 6, 10:31 pm, Alex Koshelev <[EMAIL PROTECTED]> wrote: >> Field for `author` created with `required=False`. And `clean` method >> doesn't call `clean_author` if `author` data isn't present. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: clean_* method for ModelForms?
Oh, `required=True` of course On Jul 6, 10:31 pm, Alex Koshelev <[EMAIL PROTECTED]> wrote: > Field for `author` created with `required=False`. And `clean` method > doesn't call `clean_author` if `author` data isn't present. > > On Jul 6, 10:14 pm, "Rob Hudson" <[EMAIL PROTECTED]> wrote: > > > 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 -~--~~~~--~~--~--~---
Re: clean_* method for ModelForms?
Field for `author` created with `required=False`. And `clean` method doesn't call `clean_author` if `author` data isn't present. On Jul 6, 10:14 pm, "Rob Hudson" <[EMAIL PROTECTED]> wrote: > 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 -~--~~~~--~~--~--~---