Django UserProfile ... no password

I would like to create a subset of users who do not have a login ... basically, as a way to add a photographer’s field to photos without having a full account associated with this person (since in many cases, they will never log into the site). The caveat is that I would also like to be able to enable an account for them later.

So, I think the question is becoming the best way to create a People table that is bound to the User table, without actually extending the User table with UserProfile.

+3
source share
7 answers

A user profile (how it returns django.contrib.auth.models.User.get_profile) does not expand the User table - the model that you specify as the profile model, with the parameter AUTH_PROFILE_MODULE- this is just a model from ForeignKeyto User. get_profile, and this parameter is just a convenient API for accessing an instance of a specific model that it has ForeignKeyfor a particular instance User.

Thus, one of the options is to create a profile model, in which ForeignKeyto Usercan be nulland compare the model Photowith this profile model, and not with the model User. This will allow you to create a profile for a non-existent user and attach the registered user to the profile later.

+7
source

, ? .

import random
user.set_password( str(random.random()) )

.

+3

, ( ) , . , , / ( , ).

settings.py:

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'userprofile.my_authenticate.MyLoginBackend', # if they fail the normal test
 )

userprofile/my_authenticate.py:

from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User

class MyLoginBackend(ModelBackend):
    """Return User record if username + (some test) is valid.
       Return None if no match.
    """

    def authenticate(self, username=None, password=None, request=None):
        try:
            user = User.objects.get(username=username)
            # plus any other test of User/UserProfile, etc.
            return user # indicates success
        except User.DoesNotExist:
            return None
    # authenticate
# class MyLoginBackend
+3

django auth, User, , " ". , , "" ( ).

"", User, ForeignKey ( , )

0

ForeignKey, , , , . , , , , ForeignKey AnonymousUser ( !) .

, , .

0

django.contrib.auth.models.User ( ). , , . Django .

0

inin answer: UserProfile. django.contrib.auth.models.User. , UserProfile .

0

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


All Articles