auth.User , an inadequate set of fields in it, as well as all the tricks to which it was necessary to resort.username in the username field? Do you know why? So that you can use e-mail addresses as logins.auth.User ... # models.py from django.db import models from django.contrib.auth.models import User class MyUser(User): birthday = models.DateField() request.user . Or not, and AUTHENTICATION_BACKEND . Never mind :)django_auth and our ourproj_user , which has a foreign key on django_auth . Yes, model inheritance in Django is just OneToOneField with some additional attributes. Want to use - keep in mind.Profile model with a one-to-one auth.User on auth.User ... # models.py from django.db import models from django.contrib.auth.models import User class Profile(models.Model): user = models.OneToOneField(User) birthday = models.DateField() AUTH_PROFILE_MODULE = 'accounts.Profile' profile = request.user.get_profile() profile.birthday = datetime.today() profile.save() profile , and, for example, the password of the user . No wonder and confused.get_profile() causes a query to the database. On pages where the user instance is only one (editing, for example), this is not a problem. If such a thing is, for example, in the comments, the result will be deplorable. Of course, select_related() , as you understand, will not save, because not User depends on Profile , but vice versa.User model does not mean that the associated Profile model will be created automatically. But when accessing get_profile() for a newly created user, an exception will be thrown - no doubt here. And although this trouble is treated in a few lines with the simplest signal, # profile.models from django.db import models from django.contrib.auth.models import User class Profile(models.Model): '- ' def create_profile(sender, **kwargs): if kwargs['created']: Profile.objects.create(user=kwargs['instance']) models.signals.post_save.connect(create_profile, sender=User) urls , settings or models specially instituted application), we wrote the code modifying our User: # monkey_patching.models from django.db import models from django.contrib.auth.models import User User.add_to_class('birthday', models.DateField() ) auth.User . That is, in Russian, to make a profile of the required fields. pip install django-primate pip install -e git+https://github.com/aino/django-primate.git#egg=django-primate manage.py #!/usr/bin/env python from django.core.management import setup_environ, ManagementUtility import imp try: imp.find_module('settings') # Assumed to be in the same directory. except ImportError: import sys sys.stderr.write( "Error: Can't find the file 'settings.py' in the directory " "containing %r. It appears you've customized things.\nYou'll have to " "run django-admin.py, passing it your settings module.\n" % __file__ ) sys.exit(1) import settings if __name__ == "__main__": setup_environ(settings) import primate primate.patch() ManagementUtility().execute() settings class of the model that we want to use AUTH_USER_MODEL = 'users.models.User' # users.models from django.db import models from primate.models import UserBase, UserMeta class User(UserBase): __metaclass__ = UserMeta birthday = models.DateField() # ? django.contrib.auth.models.User , it will receive the users.models.User model. The reverse is also true. The admin patch will be patched automatically, no special actions are required to connect it.auth.User :first_name and last_name , but name added;username field is 50 characters ( auth.User were 30 in auth.User )email field.get_profile method returns self , so don't worry about code using user.get_profile()username , password and email fields are best not to rename.app_label will become not users (as in the example), but auth . This is especially true for South users who may not understand for some time why ./manage.py schemamigration users --auto Source: https://habr.com/ru/post/118468/
All Articles