Custom constraints on User fields

4 views
Skip to first unread message

Nicolas Bazire

unread,
Feb 13, 2012, 2:58:28 PM2/13/12
to Django users
Hi,

I'm looking for the right way to add custom constraints on some fields
in the User model provided by django.contrib.auth. I'm not only
talking about form validation, but also database constraints (for
instance, unique).

Basically what I want to do is make the field "email" mandatory and
unique, how can I code that?

I was able to implement such "custom constraints" using pre_save
signal or a Proxy model, but I'm not really sure that either of those
implementations are a good choice.

Nicolas

Shawn Milochik

unread,
Feb 13, 2012, 3:16:19 PM2/13/12
to django...@googlegroups.com
You can override and customize the forms, then manually add the unique
indexes to your database.

Django doesn't provide much index functionality and adding indexes to
your database manually may be required for something like this or
performance.

Whenever possible update the related code with comments about the
indexes that exist in the database.

Nicolas Bazire

unread,
Feb 13, 2012, 3:25:38 PM2/13/12
to Django users
On Feb 13, 9:16 pm, Shawn Milochik <sh...@milochik.com> wrote:
> You can override and customize the forms, then manually add the unique
> indexes to your database.

Overriding the forms is only part of the problem.
For instance, forms are not used with Model.objects.create().
I want to enforce such constraints and not just at the form level.

Shawn Milochik

unread,
Feb 13, 2012, 3:27:32 PM2/13/12
to django...@googlegroups.com

Yep. Notice my response that you quoted. Manually add the unique index
to your database in addition to overriding the forms.

Nicolas Bazire

unread,
Feb 13, 2012, 3:36:47 PM2/13/12
to Django users
Okay, then new question : how to do that without creating manual
indexes? The application is supposed to be easily reusable, if I start
to include custom index creation or South migrations this is really
going to be difficult to maintain.

But that's not my main concern : how come redefining field constraints
is so difficult?

Extending the User model or monkeypatching it is highly discouraged,
but I have no clue as to how to achieve that in a clean way without
modifying the database manually.

Shawn Milochik

unread,
Feb 13, 2012, 3:44:23 PM2/13/12
to django...@googlegroups.com
It's probably related to the 80/20 rule. No tool or framework is going
to be 100% what you need. Use it for the 80%. When your needs can't be
handled by the tool, you can choose to twist yourself into a pretzel
within the constraints of the tool or break free and do it the "manual"
way -- usually with more maintainable results.

Just because there's no easy way to do something in Django doesn't mean
there's something wrong with Django. Nor does it mean that your
requirements are wrong. It just means you have to use your developer skills.

Incidentally, the auth.models.User model is a subject of a lot of debate
and inconvenience for a lot of people, and if you check out Django's
Trac instance and the history of django-developers you'll see that
people are working on changing it. Not that it will help you in this
case, but perhaps you could read up on the threads and see if you want
to get involved in the work.

Shawn


Reply all
Reply to author
Forward
0 new messages