basic unique ModelForm field for Google App Engine

Posted by Alexander Vasiljev on Stack Overflow See other posts from Stack Overflow or by Alexander Vasiljev
Published on 2009-10-01T09:06:20Z Indexed on 2010/03/16 20:01 UTC
Read the original article Hit count: 258

I do not care about concurrency issues.

It is relatively easy to build unique form field:

from django import forms

class UniqueUserEmailField(forms.CharField):
    def clean(self, value):
      self.check_uniqueness(super(UniqueUserEmailField, self).clean(value))

    def check_uniqueness(self, value):
        same_user = users.User.all().filter('email', value).get()
        if same_user:
          raise forms.ValidationError('%s already_registered' % value)

so one could add users on-the-fly. Editing existing user is tricky. This field would not allow to save user having other user email. At the same time it would not allow to save a user with the same email. What code do you use to put a field with uniqueness check into ModelForm?

© Stack Overflow or respective owner

Related posts about python

Related posts about django