Answer the question
In order to leave comments, you need to log in
How to extend User using Django REST?
It is necessary to extend the User model, for some reason the standard method does not work or I did not understand how to set up REST.
models.py
class Organization(models.Model):
name = models.CharField(verbose_name=_('Name'), max_length=45)
class UserProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, unique=True)
organization = models.ForeignKey(Organization, verbose_name=_('Organization'))
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
class UserList(generics.ListAPIView):
serializer_class = UserSerializer
def get_queryset(self):
return User.objects.all()
...
AUTH_PROFILE_MODULE = 'app.UserProfile'
...
[
{
"id": 1,
"password": "pbkdf2_sha256$24000$JtpvZFHDv9SN$aTC3Za/bRJJUvFEGUgmYo9hLKAqBmstNDXe3WZ9+a4Q=",
"last_login": "2016-01-23T14:35:57Z",
"is_superuser": true,
"username": "root",
"first_name": "",
"last_name": "",
"email": "",
"is_staff": true,
"is_active": true,
"date_joined": "2016-01-23T14:35:15Z",
"groups": [],
"user_permissions": []
}
]
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(pk=1)
>>> dir(user)
Answer the question
In order to leave comments, you need to log in
You have everything wrong.
Check out this https://docs.djangoproject.com/en/1.9/topics/auth/...
class UserProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, unique=True)
organization = models.ForeignKey(Organization, verbose_name=_('Organization'))
...
AUTH_PROFILE_MODULE = 'app.UserProfile'
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
organization = models.ForeignKey(Organization, verbose_name=_('Organization'))
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(pk=1)
>>> dir(user)
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.get(pk=1)
dir(user)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question