Django - how to handle users and profiles correctly?

I am writing a simple site that requires user and profile processing. The first initial thought is to use the django assembly in user handling, but then the user model is too narrow and does not contain the fields that I need. The user profiles are mentioned in the documentation, but the user profiles section has been removed from djangobook covering django 1.0 (ideally, the solution should work with django 1.2), and the Internet is full of different solutions without making the choice easier (for example, inheriting a user model, user profiles and django signals, etc.).

I would like to know how to write this in a good, modern, fast and safe way. Should I try to expand the django user model model, or maybe I should create my own user model wide enough to store all the information I need? Below you will find some specifications and expectations for a working solution:

  • users must be able to register and authenticate
  • Each user must have a profile (or a model with all required fields)
  • users do not need the django builtin admin panel, but they need to edit their profiles / models through a simple web form.

Please let me know how you solve these problems in your applications, and what is the best way to manage users with django. Any links to articles / blogs or code examples are greatly appreciated!

+3
source share
4 answers

users must be able to register and authenticate

django.contrib.auth- the module you need. Be sure to check your custom login form documents .

Each user must have a profile (or a model with all required fields)

You need to install settings.AUTH_PROFILE_MODULEas others note.

, 1.1 1.0. .

django builtin, / -

, ; , " " . django.contrib.auth.models.User django.contrib.auth.models.Group. , , .

EDIT: ( )...

djangobook, django 1.0 ( 1.2, 0.96) , - - ? , , ? .

djangobook ; . , Django 1.1.1; NIS.

, . Django .

, , , ( AUTH_PROFILE_MODULE),

.

, ( , , User -)?

, User.get_profile().

( "" , )?

: save().

signal - , :

from django.db.models.signals import post_save
from django.contrib.auth import User
from myUserProfileApp import UserProfile

def make_user_profile(sender, **kwargs):
    if 'created' not in kwargs or not kwargs['created']:
        return

    # Assumes that the `ForeignKey(User)` field in "UserProfile" is named "user".
    profile = UserProfile(user=kwargs["instance"])
    # Set anything else you need to in the profile, then...
    profile.save()

post_save.connect(make_user_profile, sender=User, weak=False)

. :

$ ./manage.py shell
>>> from django.contrib.auth import User
>>> from myUserProfileApp import UserProfile
>>> for u in User.objects.all():
...  UserProfile(user=u).save() # Add other params as needed.
...

, , :

>>> for u in User.objects.all():
...  try:
...   UserProfile(user=u).save() # Add other params as needed.
...  except:
...   pass
+12

Django UserProfile ( ) . settings.py AUTH_PROFILE_MODULE. , , .

, UserProfile , , Django (. ). , , , , .

User.get_profile().

github , UserProfile. , .

0

! , django dev , ( ) djangobook, , ... , django 0.96. djangobook, django 1.0 ( 1.2, 0.96) , - - ? , , ? .

0
source

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


All Articles