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