Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Custom constraints on User fields
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Nicolas Bazire  
View profile  
 More options Feb 13, 2:58 pm
From: Nicolas Bazire <nicolas.baz...@gmail.com>
Date: Mon, 13 Feb 2012 11:58:28 -0800 (PST)
Local: Mon, Feb 13 2012 2:58 pm
Subject: Custom constraints on User fields
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Shawn Milochik  
View profile  
 More options Feb 13, 3:16 pm
From: Shawn Milochik <sh...@milochik.com>
Date: Mon, 13 Feb 2012 15:16:19 -0500
Local: Mon, Feb 13 2012 3:16 pm
Subject: Re: Custom constraints on User fields
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nicolas Bazire  
View profile  
 More options Feb 13, 3:25 pm
From: Nicolas Bazire <nicolas.baz...@gmail.com>
Date: Mon, 13 Feb 2012 12:25:38 -0800 (PST)
Local: Mon, Feb 13 2012 3:25 pm
Subject: Re: Custom constraints on User fields
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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Shawn Milochik  
View profile  
 More options Feb 13, 3:27 pm
From: Shawn Milochik <sh...@milochik.com>
Date: Mon, 13 Feb 2012 15:27:32 -0500
Local: Mon, Feb 13 2012 3:27 pm
Subject: Re: Custom constraints on User fields
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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nicolas Bazire  
View profile  
 More options Feb 13, 3:36 pm
From: Nicolas Bazire <nicolas.baz...@gmail.com>
Date: Mon, 13 Feb 2012 12:36:47 -0800 (PST)
Local: Mon, Feb 13 2012 3:36 pm
Subject: Re: Custom constraints on User fields
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Shawn Milochik  
View profile  
 More options Feb 13, 3:44 pm
From: Shawn Milochik <sh...@milochik.com>
Date: Mon, 13 Feb 2012 15:44:23 -0500
Local: Mon, Feb 13 2012 3:44 pm
Subject: Re: Custom constraints on User fields
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »