Use a custom Django model or create your own model?

I am currently developing a site based on Django. For simplicity, we can assume that this is a simple community site where users can register and write messages to other users.

My current choice is to help use the custom buildin model or create something of my own. I don’t need much of the buildin User : there will be no username (email is the username), but you have set an internal name of your choice that can be used by several users (for example, Facebook), In addition, I do not need a permission system since access to others will not be based on groups. Therefore, I would use only the email fields, first name, last name and password from buildin User , and everything else would be placed in UserProfile. On the other hand, the embedded user system will be convenient for the backend of the site, since there is a possibility that I will need a permission system based on groups.

In general, it seems to me that I would rather build my one user model and use buildin only to access the administrator backend.

Is there something wrong with my thoughts?

+4
source share
4 answers

Is there something wrong with my thoughts?

Yes.

My current choice is to use a custom buildin model or create something of my own.

There is a third option.

http://docs.djangoproject.com/en/1.2/topics/auth/#storing-additional-information-about-users

everything else will be placed in UserProfile

Right.

create my one user model and use buildin only to access admin backend

Do not create your own.

Do it:

If you want to store additional information related to your users, Django provides a method for specifying a site-related model - a "user profile" - for this purpose.

+9
source

As the author of django-primate, I would like to add some comments. Django-primate, which easily allows ju to modify the built-in User model, is just for that. You may need a little more and then use a django primate.

But there are problems, although I do not think that changing the django user model in itself is a problem in general. One of the problems is that the "users" are completely different, the admin user and some other users are often not connected. This can cause problems when, for example, an administrator logs in and then wants to log in as a “regular user”, they do not expect these accounts to be connected and will not automatically register as an administrator user. It causes headaches for no reason. This also leads to many other headaches for implementing the recommended associated profile model, you often need to make sure that for each profile and profile for each contributor user there is a contributor for each user if, for example, you want to use an authentication decoder. The forms and administration of "users" make this even more cumbersome. In short: usually something will go wrong in this process at some point, it's a curse .

I basically abandoned the Contrib user model for anything other than admins. Creating a different user model is really what you want, but you also need the authentication part for this user, so the general use of django contrib User (using it for the wrong reasons). The best solution if you are in this situation is to create your own authentication for this user model. This is actually quite simple, and I cannot recommend this approach enough. I believe that the official recommendation is incorrect and that instead there should be good tools for authenticating user models embedded in django.

+2
source

You might want to take a look at the newly created django primate: https://github.com/aino/django-primate

I once created a custom model that inherits from the standard one. It works, however, I would not recommend it.

+1
source

You currently have some requirements, but they may change over time. The Django user system is fairly simple, and its use makes it easier to adapt to some of the most common use cases.
Another aspect to think about is that there are several applications that are already available and that Django users may need. Using your own model may make it difficult to use such modules.

Hacking a Django user system to fit your current requirements, on the other hand, can be tricky.
Moreover, the migration of the "User User" to "Django-User" is always possible, so you do not close this door.

All in all, I think it really depends on what you mean by "user".
If you mean registration only and don’t have real interaction with the main functions of Django, then I think that a separate model is enough, especially because you can migrate at any time with relatively little effort. However, if for your application the “user” maps something very similar to what for Django, then I would use the custom Django model.

0
source

Source: https://habr.com/ru/post/1344074/


All Articles