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.
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.
> 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.
Yep. Notice my response that you quoted. Manually add the unique index to your database in addition to overriding the forms.
On Feb 13, 9:27 pm, Shawn Milochik <sh...@milochik.com> wrote:
> On 02/13/2012 03:25 PM, Nicolas Bazire wrote:
> > 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.
> Yep. Notice my response that you quoted. Manually add the unique index
> to your database in addition to overriding the forms.
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.
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.