I have a question about django's user inheritance.
I have gone through docs of django where I have seen inheritence of User model if I want to register the user through email and password.
My questions :
What will be the difference on default User model and Custom User model that I will make ?
Can I use the permissions and add group in the Custom User model ?
And also what if I want to inherit django's default User in my model ?
class Person(User):
address = models.CharField(max_length=100)
def __unicode__(self):
return self.address
Here the user model is django's default User model ?
Can I inherit like that ?
based on django specifying a custom user model
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.
Alse Extending the existing User model can be a good option.
Related
In Django project, there is a default User model, because in the database, there is auth_user table:
So, when I create a the User model in models.py, whether I should inherit the django's User or inherit models.Model? Because I should use the permissions in my project.
EDIT
and, what's the Django's User model? if is the django.contrib.auth.models.AbstractUser?
How to custom permissions:https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#custom-permissions. And in terms of editing the default User model, you can either extend the user model or custom the user model, but normally you should extend the user model because you don't need to also custom the authentication system, django already provide a default authentication system associate with the User model. But if the default authentication system or User model doesn't fit your need, you can also make your own User model and authentication system, check this for detail: https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#substituting-a-custom-user-model
I want to implement users in my system. I know that Django already has an authentication system, and I've been reading the documentation. But I don't know yet the difference between
from django.contrib.auth.models import User
class Profile(User):
# others fields
And
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User)
# others fields
I don't want to know why to use one or another, but what happens under the hoods. What's the difference?
Your first example is multi-table inheritance.
class Profile(User):
If you have a profile, you can access all the fields on the user model directly (e.g. profile.username and profile.email). In this case, Django creates a OneToOneField for you automatically.
The second example is a regular OneToOneField.
class Profile(models.Model):
user = models.OneToOneField(User)
In this case, you cannot access profile.username and profile.email. Instead, you access these fields via the one to one field (e.g. profile.user.username and profile.user.email).
In your case, where you are adding a profile model, I would avoid using inheritance, and use a one to one field instead. The User model has custom admins to handle passwords. If you use multi-table inheritance, then your Profile model would have to handle this as well. By using a one-to-one field, the custom admins can handle the user fields, and your Profile model admins only have to handle the additional profile fields.
Another option is creating a custom user model. In this case you subclass an abstract class AbstractUser or AbstractBaseUser instead of the class User. If your Profile class works, then I would recommend this instead of the custom user model, because custom user models are more complicated to set up.
I have different apps in my django project. I want to keep an independent app for auth&auth that is reused in all apps. The auth&auth app is one app with my basic custom user model. This user needs to log in with email so I am inheriting from AbstractBaseUser instead of extending the User class. Now in all apps the login is same way, basic user attributes are same; but some apps add additional fields like 'salary', others use the model as is. So what is the best way to reuse the Custom User model I created in my login app? If I inherit the concrete class, I'll have 2 tables. If I declare the Custom User class abstract, how will I use its functionalities, roles and permissions?
I think the best way to achieve what you are asking for is:
class Learners(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, unique=True)
nick = models.CharField(max_length=12, unique=True)
#Many to Many relationship with course
courses = models.ManyToManyField(Course, blank=True)
def __str__(self):
return self.user.get_full_name()
Where the AUTH_USER_MODEL is set to the Custom User model, inheriting from AbstractBaseUser, that you created, like this : AUTH_USER_MODEL = 'courseSystem.MyUser' in settings.py for the main Django project. In my example courseSystem.MyUser is the Custom User Class defined.
I am sharing this from a similar situation you have described above, that I am working on.
In the above example, if you were to make the custom MyUser class as abstract and then inherit the Learners class from it:
class Learners(MyUser):
then Learners class would no longer be a models.Model type class but the concrete User class for that app, and in each case i.e., for each such class ineheriting from the abstract MyUser class, you would have to overload the necessary members of MyUser required for a concrete user class for Django. Also since you are using AbstractBaseUser and not extending on default User class, you would have to define custom User forms, and User Managers for your custom user class. So if you have a concrete MyUser class, as in the example above, you can define the custom User Manager and User Forms for MyUser once, and keep one-to-one relationship to it from your models in different apps. But if you make the MyUser class as abstract, you wouldn't define User Forms and User Managers for MyUser, but separately for each of your apps where you form a concrete user class by inheriting from this abstract Custom User class.
https://docs.djangoproject.com/en/1.9/topics/auth/customizing/
I want to implement users in my system. I know that Django already has an authentication system, and I've been reading the documentation. But I don't know yet the difference between
from django.contrib.auth.models import User
class Profile(User):
# others fields
And
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User)
# others fields
I don't want to know why to use one or another, but what happens under the hoods. What's the difference?
Your first example is multi-table inheritance.
class Profile(User):
If you have a profile, you can access all the fields on the user model directly (e.g. profile.username and profile.email). In this case, Django creates a OneToOneField for you automatically.
The second example is a regular OneToOneField.
class Profile(models.Model):
user = models.OneToOneField(User)
In this case, you cannot access profile.username and profile.email. Instead, you access these fields via the one to one field (e.g. profile.user.username and profile.user.email).
In your case, where you are adding a profile model, I would avoid using inheritance, and use a one to one field instead. The User model has custom admins to handle passwords. If you use multi-table inheritance, then your Profile model would have to handle this as well. By using a one-to-one field, the custom admins can handle the user fields, and your Profile model admins only have to handle the additional profile fields.
Another option is creating a custom user model. In this case you subclass an abstract class AbstractUser or AbstractBaseUser instead of the class User. If your Profile class works, then I would recommend this instead of the custom user model, because custom user models are more complicated to set up.
I am really stuck in my project right now. I am trying to implement Oauth2 for my app. I found out about django-oauth2-provider a lot and tried it. The only problem is, it uses the User model at django.contrib.auth. The main users of our site are saved in a custom model called User which does not inherit from or extend the model at django.contrib.auth.
Is there any way to use my custom User model for creating clients and token?
If django-oauth2-provider can't be used for this purpose, can anyone recommend me some oauth2 library with the option to implement oauth2 with my own model.
Sincerely,
Sushant Karki
As the previous answer suggested, you should extend AbstractUser from django.contrib.auth.models.
The problem with the access token that the OP referring to, occur when changing the setting AUTH_USER_MODEL AFTER django-oauth2-provider was migrated.
When django-oauth2-provider is migrated, it creates a key constrain between the User model and django-oauth2-provider.
The solution is very easy:
Create your new User model and change the AUTH_USER_MODEL setting.
Go to the django_migration table in your database.
Delete all rows of django-oauth2-provider.
run python manage.py makemigrations
run python manage.py migrate
Now, the django-oauth2-provider tables are connected to the RIGHT User model.
django-oauth2-provider fetches the user model using settings.AUTH_USER_MODEL, with a fallback to auth.User. If you extend AbstractUser your User model will include all the fields of auth.User plus any additional fields you specify.
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
some_additional_field = models.BooleanField(default=False)
Specify the user model to be used like this in settings.py:
AUTH_USER_MODEL = 'user_api.User'
If you don't want to base your user on AbstractUser you'll also need to write your own user manager, e.g. by extending the BaseUserManager
You can read more about ways to customize django's user model here.