I have a question regarding designing models for the User.
Is it a better approach if you :
create an AbstractUser model and put all extra attributes (eg: phone, mobile, fax) inside the model
v.s
creating a separate model called UserProfile and link it up with the Member using OneToOneField ?
The answer is in the documentation:
If you’re entirely happy with Django’s User model and you just want to
add some additional profile information, you could simply subclass
django.contrib.auth.models.AbstractUser and add your custom profile
fields, although we’d recommend a separate model as described in the
“Model design considerations” note of Specifying a custom User model.
AbstractUser provides the full implementation of the default User as
an abstract model.
If you just want to store extra information for the user then use a OneToOneField.
But, if you want behavioural changes in your user model, for example, you want the email address or phone number to be used for login, instead of username, or something like that, then inherit from AbstractUser and create a custom user model.
My suggestion is for learning purpose use AbstractUser and AbstractBaseUser whenever you CAN even if u don't need it.
Related
I am a newbie to web developmnet. I'm using django and lately I have been realy confused and undecided on wether I should use a Custom user model with additional fields I need or create a seperate User profile model with one to one relationship with the user model. What is the best practice?. In which ever case is best I would like to aslo add the fields like 'followers', 'following', 'likes', 'connections' and users should be able to easily edit some details on their accounts after creating, which method is the best?
I would suggest you go for a Profile model. Depending on your requirements, make a little research, if Django's AbstractUser class has all the fields you want, Use that, it will allow you to add more fields as well as you continue to build and it's a good practice. But if AbstractUser has more fields than required, use Django's AbstractBaseUser to specify only the fields you want.
Then create a Profile model with the details you listed above, "likes", "connections", "followers", "following," and you can keep on adding more if you like. Use the models.OneToOneField to specify that a user model can have just one Profile model.
This should make everything a bit easier for you.
This is for a practice project I am building to learn Django. The site will need to track certain data about the user. Should I customise the default User model? Or create a new model like -
class UserData(models.Model):
'''holds user info'''
owner = models.ForeignKey(User)
# data
I couldn't find a clear answer.
It really depends. I would recommend keeping User model small, and put most of the additional information in some kind of Profile model. However, there are certain important things that you may want to place in the User model, for example:
user_type field - you may have multiple Profile models (think CustomerProfile, VendorProfile etc.) and you need a way to distinguish User and grant appropriate access to them
Something related to authorization, like require_2fa field
If you are starting a new project and don't expect a lot of additional info, you may keep them in the User model just for simplicity, especially if you are already customizing it (i.e. to replace username with email)
Users are special so it is better to use django.contrib.auth.models.AbstractUser to keep the the default user model behaviour and add your custom fields there.
from django.contrib.auth.models import AbstractUser
class CustomUser(AbstractUser):
pass
I've wrote an article about it that I think it would be userful in this case.
I am building a food ordering website with Django. I want my users to register an account on my site, and they should sign in to actually order. I want to use the User class built in with Django, but that doesn't include necessary fields like address, confirmation ID, and phone number. If I build a custom User model, that doesn't have many good helper functions which I can use like auth.authenticate. I searched this topic, and I found that I could use AbstractUser. But when I inherited my CustomUser class from AbstractUser, some strange things began to happen. After some more research, I found out that changing the User model after applying my built-in migrations give some errors as there are some relationships or something.
I deleted my database and created a new one. Now, I am extending my CustomUser class from the built-in User class. This works fine, only you can't do auth.authenticate checking with the, confirmation ID for instance. Also, it seems to create two models every time I create a new CustomUser, the other on in the Users under the auth tab.
Can you tell me any good way to connect the User model with a few more fields after applying the built-in migrations? Thanks in advance.
You should extend from AbstractUser and not User class ( behaviour you are experiencing is Multi-table inheritance (as documented))
Whole process of substituting default user model is well documented
Everywhere, everyone seems to be using "custom user model" derived from either AbstractUser or AbstractBaseUser class.
Why nobody directly sub-classes the user model from models.Model?
Won't that be much easier and simpler?
What additional functionality does using these custom user models provide?
As the Django Docs state:
The easiest way to construct a compliant custom user model is to inherit from AbstractBaseUser. AbstractBaseUser provides the core implementation of a user model, including hashed passwords and tokenized password resets. You must then provide some key implementation details.
The AbstractBaseUser doesnt come with fields apart from password, last_login and is_active. You use AbstractBaseUser when you want to implement a custom user and use different fields and/or authentication than the ones AbstractUser comes with.
AbstractUser subclasses AbstractBaseUser and provides a base implementation of an User model. With AbstractUser you basically use the User model with all its fields, but it comes with extra inherited functions and Django's implementation of authentication. You use AbstractUser when you are happy with the way Django provides an User and handles authentication.
Django - Models extension Vs User Profile
I want add some custom fields like following
1. ssn
2. is_manager
3. manager
I have 2 choices -
Extend AbstractBaseUser
OR
Create User profile based on signal and have OnetoOne field.
Which one is better, future proof, DB migration friendly and maintainable ?
The Django documentation answers this question in detail:
If you wish to store information related to User, you can use a OneToOneField to a model containing the fields for additional information. This one-to-one model is often called a profile model, as it might store non-auth related information about a site user.
In your case, the added fields do not seem to be authentication related, so your best bet is to use the user profile method. Substituting a custom user model is overkill for such purposes.