OneToOneField
connection.AbstractBaseUser
AbstractBaseUser
. Requires extra care and setting changes in settings.py
. Ideally, it should be done at the beginning of the project, as it will significantly affect the database schema.AbstractUser
AbstractUser
. Requires extra care and setting changes in settings.py
. Ideally, it should be done at the beginning of the project, as it will significantly affect the database schema.models.py
from django.contrib.auth.models import User from .managers import PersonManager class Person(User): objects = PersonManager() class Meta: proxy = True ordering = ('first_name', ) def do_something(self): ...
User
model to the Person
model. We say Django is a proxy model by adding the following property inside class Meta
: Proxy = True
do_something()
method is defined.User.objects.all()
and Person.objects.all()
will query the same database table. The only difference is in the behavior that we define for the proxy model.models.py
from django.db import models from django.contrib.auth.models import User class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(max_length=500, blank=True) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(null=True, blank=True)
Profile
model is automatically updated when creating / changing User
model data. from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save from django.dispatch import receiver class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) bio = models.TextField(max_length=500, blank=True) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(null=True, blank=True) @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_profile(sender, instance, **kwargs): instance.profile.save()
create_user_profile()
and save_user_profile()
on the User
model save event. This signal is called post_save
.Profile
data: <h2>{{ user.get_full_name }}</h2> <ul> <li>Username: {{ user.username }}</li> <li>Location: {{ user.profile.location }}</li> <li>Birth Date: {{ user.profile.birth_date }}</li> </ul>
def update_profile(request, user_id): user = User.objects.get(pk=user_id) user.profile.bio = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit...' user.save()
Profile
save methods. All this is done using the User
model.forms.py
class UserForm(forms.ModelForm): class Meta: model = User fields = ('first_name', 'last_name', 'email') class ProfileForm(forms.ModelForm): class Meta: model = Profile fields = ('url', 'location', 'company')
views.py
@login_required @transaction.atomic def update_profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, _('Your profile was successfully updated!')) return redirect('settings:profile') else: messages.error(request, _('Please correct the error below.')) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, 'profiles/profile.html', { 'user_form': user_form, 'profile_form': profile_form })
profile.html
<form method="post"> {% csrf_token %} {{ user_form.as_p }} {{ profile_form.as_p }} <button type="submit">Save changes</button> </form>
select_related()
method is select_related()
. users = User.objects.all().select_related('Profile')
AbstractBaseUser
extensionauth token
, and username
absolutely not needed. In addition, there was no need for the is_staff
flag since I did not use Django Admin. from __future__ import unicode_literals from django.db import models from django.contrib.auth.models import PermissionsMixin from django.contrib.auth.base_user import AbstractBaseUser from django.utils.translation import ugettext_lazy as _ from .managers import UserManager class User(AbstractBaseUser, PermissionsMixin): email = models.EmailField(_('email address'), unique=True) first_name = models.CharField(_('first name'), max_length=30, blank=True) last_name = models.CharField(_('last name'), max_length=30, blank=True) date_joined = models.DateTimeField(_('date joined'), auto_now_add=True) is_active = models.BooleanField(_('active'), default=True) avatar = models.ImageField(upload_to='avatars/', null=True, blank=True) objects = UserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = [] class Meta: verbose_name = _('user') verbose_name_plural = _('users') def get_full_name(self): ''' Returns the first_name plus the last_name, with a space in between. ''' full_name = '%s %s' % (self.first_name, self.last_name) return full_name.strip() def get_short_name(self): ''' Returns the short name for the user. ''' return self.first_name def email_user(self, subject, message, from_email=None, **kwargs): ''' Sends an email to this User. ''' send_mail(subject, message, from_email, [self.email], **kwargs)
AbstractBaseUser
we must follow some rules:USERNAME_FIELD
- string with the name of the model field, which is used as a unique identifier ( unique=True
in the definition);REQUIRED_FIELDS
- a list of field names that will be requested when creating a user using the createsuperuser
control createsuperuser
is_active
is a Boolean attribute that indicates whether the user is considered “active”;get_full_name()
is a long description of the user: it is not necessarily the full name of the user, it can be any string that describes the user;get_short_name()
- short description of the user, for example, his name or nickname.UserManager
. Because the existing manager defines the create_user()
and create_superuser()
methods. from django.contrib.auth.base_user import BaseUserManager class UserManager(BaseUserManager): use_in_migrations = True def _create_user(self, email, password, **extra_fields): """ Creates and saves a User with the given email and password. """ if not email: raise ValueError('The given email must be set') email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, email, password=None, **extra_fields): extra_fields.setdefault('is_superuser', False) return self._create_user(email, password, **extra_fields) def create_superuser(self, email, password, **extra_fields): extra_fields.setdefault('is_superuser', True) if extra_fields.get('is_superuser') is not True: raise ValueError('Superuser must have is_superuser=True.') return self._create_user(email, password, **extra_fields)
UserManager
from the username
and is_staff
.settings.py
: AUTH_USER_MODEL = 'core.User'
core
.Course
: from django.db import models from testapp.core.models import User class Course(models.Model): slug = models.SlugField(max_length=100) name = models.CharField(max_length=100) tutor = models.ForeignKey(User, on_delete=models.CASCADE)
from django.db import models from django.conf import settings class Course(models.Model): slug = models.SlugField(max_length=100) name = models.CharField(max_length=100) tutor = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
AbstractUser
extensiondjango.contrib.auth.models.AbstractUser
class provides the full default user model implementation as an abstract model. from django.db import models from django.contrib.auth.models import AbstractUser class User(AbstractUser): bio = models.TextField(max_length=500, blank=True) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(null=True, blank=True)
settings.py
: AUTH_USER_MODEL = 'core.User'
from django.conf import settings
and using settings.AUTH_USER_MODEL
instead of directly referring to the User
class.AbstractBaseUser
— an out-of-box authentication process does not suit you.AbstractUser
- you are satisfied with the authentication process, but I would like to add additional user data (the data will be stored directly in the user model, and not in a special model).Source: https://habr.com/ru/post/313764/
All Articles