D
D
Dmitry2016-01-23 19:54:53
Django
Dmitry, 2016-01-23 19:54:53

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'))

serializers.py
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

views.py
class UserList(generics.ListAPIView):
    serializer_class = UserSerializer
     
    def get_queryset(self):
        return User.objects.all()

settings.py
...
AUTH_PROFILE_MODULE = 'app.UserProfile'
...

As a result of this I get this:
[
    {
        "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": []
    }
]

No hints of UserProfile ...
If you exit in the shell, then there is nothing about get_profile
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(pk=1)
>>> dir(user)

I would be grateful for any help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Klimenko, 2016-01-23
@pyHammer

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'

This is fundamentally wrong, you did not extend the user model, you just created a certain model that refers to the user - this is not true!
You need to inherit from the standard model and add a new field already there:
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    organization = models.ForeignKey(Organization, verbose_name=_('Organization'))

It is best to do this when starting a new project, if users in the system are already used, you cannot just take and turn on a new custom model, everything will break for you, there is a good solution how to migrate painlessly: https://pypi.python.org/pypi/ django_custom_user_mi... Used
it about a month ago, everything is fine, just make backups and follow the instructions exactly!
After new custom users appear, you will no longer be able to make requests like this:
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(pk=1)
>>> dir(user)

You'll have to do something like this:
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.get(pk=1)
dir(user)

And so everywhere where you used to use django.contrib.auth.models:User

R
Roman Kitaev, 2016-01-23
@deliro

Here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question