Thursday, May 29, 2014

Re: How to create a custom user model ?

The django documentation has a full example with code of using custom user models here

On Thursday, May 29, 2014 8:06:24 AM UTC-5, Geotribu wrote:
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





--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/4e042da5-1fb5-4a74-88d7-276023d9c1a7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment