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.
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.
Would it be more efficient to create a custom user model if I want settings to be tied to the user (AbstractUser would be used)?
The other seemingly simpler option would be to create a Settings model in my main app and tie it to the user with a foreign key. Which would be more maintainable when the user-base grows?
Some examples of a few settings options would be private profile, hidden in search, profile pictures.
1. Create a Custom User Model:
You should create a custom User Model extending AbstractUser when you use Django authentication process and need to add some extra information directly in the User model, without having to create another class.
2. Create a Settings Model in my main App and tie it to the User Model with a OneToOneField:
You should use a One-To-One Link as long as you have to store extra information about the existing User Model and it doesn't have anything to do with the authentication process.
Reference: https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html
this is my first question on stackoverflow. I am a beginner programmer and kind of have issues with programming logic.
My issue is that I have a model(which happens to be a form) which collects important information from the users, I want to be able relate this model with the individual user since it has the information about them that I need.
Any form of help is appreciated...By the way am using the Django web framework.
Before 1.5: https://docs.djangoproject.com/en/dev/topics/auth/#auth-profiles, add a model that links OneToOne to the User model provided by Django and telling about that model in settings.py with the global AUTH_PROFILE_MODULE
After 1.5: https://docs.djangoproject.com/en/dev/topics/auth/#auth-custom-user, the previous method is deprecated. Now you have to fully customize the User model provided by Django.
If you are a new user, I suggest the following links:
EXTENDING USER MODEL IN DJANGO
Storing additional information about users
Generally, you create a normal model with a foreign key to the Django User model. Then add any other fields you would want to store for a user e.g. date of birth, website, favorite color, etc.
I just started a Django project (there are no apps in it). I activated the admin in settings file and can access the Django administration page. There is a column in Django page to add users; while adding users I get only three fields under personnal info, but I need to store some more information about users. I Googled around and found that I can use user profiles to accomplish this. I tried, but I am having problems.
My aim is to add three more fields to the user table:
role
contact number
other
I need details like: which function I need to write and where to do this.
I found this, but I do not know where I need to write these steps. I would greatly appreciate a more clear explanation of this.
Django User Profiles is what you need. The blog you linked to has clear steps on how to do it. You can check out the Django documentation. http://www.turnkeylinux.org/blog/django-profile also provides a good explanation.
Basically you need to create a new model with User as ForeignKey and define the model in the settings.py as AUTH_PROFILE_MODULE = "django_app.your_profile_modelname". Create the profile and save it just like any other model, and access it using user.get_profile()
Adding a couple of things in response to your questions below:
First, do not create apps as a directory. Use startapp <appname> [destination] as described here. That will create the app directory.
Second, you have to add the app to INSTALLED_APPS in the project's settings file, do a syncdb. Basically, follow the steps in Django tutorial on writing your first app.
Third, UserProfile is a separate model. It is not an extension of User. It is associated with the User just because you added User as the ForeignKey.
Fourth, to be able to see the user profile model in admin, you do exactly what you would do to add any other model to admin page. Create a file names admin.py under your app with:
from django.contrib import admin
from myproject.app.models import UserProfile
admin.site.register(UserProfile)
There are three key concepts to understand:
There is no built in "profile" system in Django, beyond the limited auth app which is really geared just to user login. You are expected to roll your own.
There is nothing magical about a profile record in itslef, it is just like any other record that takes User as a foreign key (or, more properly, a one-to-one field as per the docs). You create it by creating a custom django app (traditionally called profiles) and a model for that app (traditionally called UserProfile, since Profile is not allowed as a model name).
The only thing that sets UserProfile aparts as a model is that you specify it as the AUTH_PROFILE_MODULE which means that it is accessible when called .get_profile() on a User record. That's it. If you set up the UserProfile like so:
def UserProfile(models.Model):
user = models.OneToOneField(User, related_name='profile')
other fields
then you can also access the profile as user.profile rather than user.get_profile() which some people prefer.
Again, nothing magical about the profile model -- it is just a model record like any other model record.
If you want to be able to edit additional fields within the user form that's more complicated; easiest way is probable unregister User and then register it again using your custom ModelAdmin and form class but judging by your question you're probably not at that level yet.