⬆️ ⬇️

User profiles: pros, cons, pitfalls

It's no secret that working with user profiles in Django can only be called a misfortune. We all faced with the solidity of the model auth.User , an inadequate set of fields in it, as well as all the tricks to which it was necessary to resort.



Everyone had to pervert: not only junga users, but also its core developers themselves. Remember, for example, how in Django 1.2 it suddenly became possible to use dog characters (@) and username in the username field? Do you know why? So that you can use e-mail addresses as logins.



We, ordinary users, also lived hard. In order to change the user profile, adding to it some interesting fields - a seemingly ordinary thing, right? - had to act in different ways.



By the way, about monkeys ...



The guys who gave the world the legendary sorl.thumbnail , once again distinguished themselves and produced at the expense of another killer thing. Meet: django-primate , an application that using monkeypatching methods (primates and monkeys, do you feel the correlation?) Makes it very easy to turn your own model into auth.User . That is, in Russian, to make a profile of the required fields.



Getting started is easy enough. First you need to put django-primate. Possible with PyPI:



 pip install django-primate 


... or the latest version from their repository:



 pip install -e git+https://github.com/aino/django-primate.git#egg=django-primate 


You need to call the patch at the start. The creators recommend using it in 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() 


it now remains only to indicate in the settings class of the model that we want to use



 AUTH_USER_MODEL = 'users.models.User' 


... and you can start to invent models:



 # users.models from django.db import models from primate.models import UserBase, UserMeta class User(UserBase): __metaclass__ = UserMeta birthday = models.DateField() #    ? 




Now every time a project component 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.



By default, the django-primate user model has the following differences from auth.User :



For the rest, the primate user model walks like a duck, floats like a duck and quacks like a duck behaves very much like the original source.



Of course, you can use absolutely any set of fields, but in this case there is a risk that there will be incompatibility between third-party applications, so the username , password and email fields are best not to rename.



One more thing: for compatibility with jung, the primate will model the model so that its 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 


does not create any migrations.



However, everyone can get acquainted with the README, which I oochen translated freely. If I missed something, made a mistake or made an inaccuracy, write in comments or share your thoughts on the subject.



Thanks for attention :)

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



All Articles