How to create a custom user model ?

73 views
Skip to first unread message

Geotribu

unread,
May 29, 2014, 9:06:24 AM5/29/14
to django...@googlegroups.com
Hi Guys,

I've developed a couple of website using Django, but this is the first time I must develop one that integrates a user authentication process and the possibility to have some roles.
Thanks to Django's flexibility, it looks like very easy to do it and pretty straightforward.
From my understanding there are two ways to extend Django's User Modl:
  • The firs one uses the AbstractUser class and allow to extends the current Django's User Model by adding some additional profile information
  • The second one uses AbstractBaseUser that only keeps the most important fields of the Django's User Model (e.g. password)

For my project, I've followed the documentation and I've extended the current Django's User Modelusing the AbstractUser class.
Lets take the following example of a user and for all of them a company :

In one of my model :
class CustomUser(AbstractUser):
        company_name = models.CharField(max_length=255)

In my settings.py:
AUTH_USER_MODEL = 'myCustomuserApp.CustomUser'

Then I use the syncdb command line, to update my database. Regarding this process I've got a couple of questions:
  1. After the syncdb command I can't see anymore the users in the admin interface. As I'm using the AbstractUser, I thought in the beginning that everything will stay as it was before (without any custom model) and the new fields would been automatically added to the User Model. But, it looks like even with AbstractUser, Im' overwriting the current Django's User Model so I must manually add it register this model in my admin Django's part: admin.site.register(CustomUser). Is it the right process to do this ?
  2. After my new model has been registered in the admin interface, I can see the list of my users, but when I try to add a new one using the admin interface Django generates an error. I suppose that I must probably create my own creation and change forms.

But before I go any further, I just want to be sure that I'm doing the right thing. From what I was understanding, by using the AbstractUser I would not have to do any changes to the core structure of my database (basically everything would stay the same, and the new fields would have been added to the core auth user table). So I was thinking that I don't need as well to implement new forms and the only time I need to do that it's when I use the AbstractBaseUser class. It looks like I'm wrong.

So could someone please tell me the difference between this two classes (AbstractUser , AbstractBaseUser ) ?

And if I need to implement the creation and modification form, do you have any good (and simple) example on how to do this ?


Thanks a lot for your advices.

Regards,

Arnaud





C. Kirby

unread,
May 29, 2014, 11:54:04 AM5/29/14
to django...@googlegroups.com
The django documentation has a full example with code of using custom user models here

Geotribu

unread,
May 29, 2014, 12:16:39 PM5/29/14
to django...@googlegroups.com
Hi,

Thank you for your answer and this interesting link.
But in this example, the class used is AbstractBaseUser. Can I just follow this example if I use AbstractUser.

In fact my initial question was more related to a general understanding of Django and the differences between AbstractBaseUser and AbstractUser.

Thanks again

Arnaud

C. Kirby

unread,
May 29, 2014, 1:13:09 PM5/29/14
to django...@googlegroups.com
Ah.
AbstractUser (AU) inherits from AbstractBaseUser (ABU).
ABU only provides password and last login fields. You should use it as your base class if you want to do something radically different from the  django user. The best example would be using email addresse for the username field.A standard Django User model is just a wrapper around the AU model. If you are happy with what the standard user is (Username, first, last, email, is_staff, is_admin, date joined, and the abstract base user fields) but want to add a few new fields, like in your case company_name.

For your questions:
1. you are correct that you need to register the custom user to the admin. You will also have to write an admin class as shown in the example. You will do it by admin.site.register(CustomUser, CustomUserAdmin)
2. Also correct. Just take the default forms and add the company_name field to them. Also just follow the example I linked to

Hope this helps some,
Kirby

Arnaud Vandecasteele

unread,
May 29, 2014, 5:34:52 PM5/29/14
to django...@googlegroups.com
Hi Kirby,

Thank you very much for these detailed information.
Finally, I've went to the source code of Django's auth and I understand a bit better the process.

Thanks again

A.


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/I1-goX7LUA0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f89b0570-139f-4ddd-8ce7-9198b64dfc91%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
--------------------------------------------------------------------
Arnaud Vandecasteele
SIG - WebMapping - Spatial Ontology - GeoCollaboration

Web Site
http://www.marinegis.com/?page_id=131
http://geotribu.net/
Reply all
Reply to author
Forward
0 new messages