Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.5 override abstractuser email field to be required and unique

I have a custom user model extending AbstractUser. I have added two custom fields. But i want the default email field to be uniqe and required . I have searched a lot on the net, but could not find a good advice.

Is it the right way to do that in extended usercreationform with overridden 'clean_email' method?

Thanx

like image 767
ratata Avatar asked Feb 15 '13 23:02

ratata


1 Answers

Im facing the same problem, I'm extending the AbstractUser as I only need to add info. This was the only thing that worked for me:

AbstractUser._meta.get_field('email')._unique = True

That solves the unique problem, but still I can't make it required, when you extend AbstractUser it email field IS NOT, it can be noticed if you add a extended user from the admin and also because the forms dont take it that way.

I've tried

AbstractUser._meta.get_field('email')._blank = False
AbstractUser._meta.get_field('email')._null = False

But it doesn't seem to work.

EDIT: For some reason unique works with '_', but blank and null don't, I just tried this

AbstractUser._meta.get_field('email').blank = False
AbstractUser._meta.get_field('email').null = False

And it worked!

like image 57
steven2308 Avatar answered Sep 28 '22 03:09

steven2308