+1 it works, perfect (I just change the self.cleaned_data into
self.clean_data). Now when you showed me the trick I was able to track
it in docs as well, unfortunately I'm using 0.96 and
http://www.djangoproject.com/documentation/0.96/newforms/ is much
different to http://www.djangoproject.com/documentation/newforms/

Is there anything coming into django that would provide validation of
unique=True out of the box?

Anyway thank you very much


On Jul 18, 8:42 pm, Nathan Ostgard <[EMAIL PROTECTED]> wrote:
> You want to create a BaseForm to specify in your form_for_model call.
> You could do something like:
>
> from django import newforms as forms
>
> class MyBaseForm(forms.BaseForm):
>   def clean_myfield(self):
>     if
> MyModel.objects.filter(myfield=self.cleaned_data['myfield']).count():
>       raise forms.ValidationError('some error message')
>
> MyForm = forms.form_for_model(MyModel, form=MyBaseForm)
>
> --------
> Nathan Ostgard
>
> On Jul 18, 7:06 am, stereoit <[EMAIL PROTECTED]> wrote:
>
> > Hi,
> > I have model with field that has attribute unique set to True. I know
> > newforms are not able to handle such a validation as this is DB
> > related. However I read at newforms doc page that it is possible to
> > provide method called clean_<field>() that can do custom validation.
> > My problem is that method is supposed to access the data via
> > self.cleaned_date and I create form by using form_for_model hence I do
> > not know how to write such a method.
>
> > Can anyone point me to right direction or best practice? Handling this
> > at form.save() seem not right to me.
>
> > My idea was to create custom validation method that would try tu pull
> > object from DB with <fieldname> set to same as in
> > cleaned_data['fieldname'] and if it exists it would raise
> > ValidationError. But I have no clue how to do that at the moment.


--~--~---------~--~----~------------~-------~--~----~
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