My solution is to use the base class parameter of form_for_[model|instance] function, for example:
class SomeTest(models.Model): name = models.CharField(maxlength=50) user = models.ForeignKey(User) class Meta: unique_together = (('user', 'name'),) class SomeTestBaseForm(forms.BaseForm): def __init__(self, *args, **kwargs): super(SomeTestBaseForm, self).__init__(*args, **kwargs) def clean_name(self): if self.data.get('name', None) and self.data.get('user', None): try: sometest = SomeTest.get(name=self.data['name']) except SomeTest.DoesNotExist: return self.data['name'] raise forms.ValidationError(u'The name already exists') SomeTestForm = forms.form_for_model(SomeTest, SomeTestBaseForm) this way you can add custom validation and DRY when your form's fields match your model. Regards, Ali On 5/29/07, ringemup <[EMAIL PROTECTED]> wrote: > > > Hello -- > > I'm using a basic form_for_model() form object for a model that has a > unique=True constraint on a field other than the primary key. > > When validating submitted data, is there a reason the form check that > that constraint hasn't been violated and throw a validation error? > I'd like to be able to catch that input error and present a message to > the user, instead of what's currently happening, which is that no > error is thrown and when I try to save the data I get an integrity > error, which is unrecoverable. > > Do I need to create a custom form class just to handle a uniqueness > requirement? > > Thanks! > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---