Django 1.8+ extends User model

I know this question has been asked hundreds of times, but most of them contain accepted answers that are no longer valid. Some of them are for Django 1.5, some are even older.

So, I am looking for a relevant answer. The question that most resembles my problem is this .

I am using a module django-registrationand I want users to have additional fields (e.g. integer user.points). I am fine with my own model MyUserand modify the rest of my program accordingly. However, I doubt that this will work in accordance with the registration module. Therefore, I suppose that the ideal way would continue to use the model Userand somehow relate them so that whenever an object Useris created, the corresponding object is created MyUserwith default values ​​for additional fields.

-How can I do this with Django 1.8+?

Thanks for any help

+1
source share
3

: Django

One-To-One:

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name="profile")
    #more fields

    @staticmethod
    @receiver(post_save, sender=User, dispatch_uid="at_user_post_save")
    def user_post_save(sender, instance, **kwargs):
        #create new profile on user.save() (if necessary)
        profile, new = UserProfile.objects.get_or_create(user=instance)


#in view
profile = request.user.profile

UPDATE:

- ? , , UserProfile?

UserProfile - , , no user . , , user on_delete kwarg.

?

PK, UserProfile PK user.

OneToOneField ForeignKey unique=True, , 0/1, DoesNotExist None, null=True.

, , , 1 , user.profile. .select_related('profile'), user auth, , .

+1

django-registration , django-registration .

, , registration.forms.RegistrationForm, RegistrationView. email is_active django email_user . USERNAME_FIELD . .

django-registration docs.

+1

: django, : "python manage.py migrate".

, , un-extanded user , django , .

, , , , .py:

AUTH_USER_MODEL = 'APPNAME.Account'

. " " , .

accountb. :

from django.db import models
from django.contrib.auth.models import UserManager
from django.contrib.auth.models import AbstractUser

class AccountManager(UserManager):
    def create_user(self, email, password=None, **kwargs):
        if not email:
            raise ValueError('Users must have a valid email address.')
        if not kwargs.get('username'):
            raise ValueError('Users must have a valid username.')

        account = self.model(
            email=self.normalize_email(email), 
            username=kwargs.get('username'), 
            year_of_birth = kwargs.get('year_of_birth'),
            #MODEL = kwargs.get('MODEL_NAME'),
        )
        account.set_password(password)
        account.save()

        return account

    def create_superuser(self, email, password, **kwargs):
        account = self.create_user(email, password, **kwargs)
        account.is_staff = True
        account.is_superuser = True
        account.save()

        return account

class Account(AbstractUser):
    email = models.EmailField(unique=True)
    #ADD YOUR MODELS HERE

    objects = AccountManager()

    def __str__(self):
        return self.email

, admin.py:

from django.contrib import admin
from .models import Account

admin.site.register(Account)
+1

Source: https://habr.com/ru/post/1658548/


All Articles