V
V
Vitaly Ananiev2022-04-02 19:50:07
Django
Vitaly Ananiev, 2022-04-02 19:50:07

Created an application and a Model for tracking online users link to the lesson https://evileg.com/en/post/546/ gives an error when creating migrations?

settings.py

INSTALLED_APPS = [
'my_auth_project.apps.MyAuthConfig',
...
]
AUTHENTICATION_BACKENDS = (
    'my_auth_project.backends.MyBackend',
    'django.contrib.auth.backends.ModelBackend',
)

modes.py
from django.contrib.auth.models import AbstractUser
from django.contrib.humanize.templatetags.humanize import naturaltime
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _


class User(AbstractUser):
    last_online = models.DateTimeField(verbose_name='Online', blank=True, null=True)

    def is_online(self):
        if self.last_online:
            return (timezone.now() - self.last_online) < timezone.timedelta(minutes=15)
        return False

    def get_online_info(self):
        if self.is_online():
            return _('Online')
        if self.last_online:
            return _('Last visit {}').format(naturaltime(self.last_online))

        return _('Unknow')

backends.py
from django.contrib.auth import get_user_model
from django.utils import timezone
from django.contrib.auth.backends import BaseBackend


class MyBackend(BaseBackend):
    def get_user(self, user_id):
        try:
            user = get_user_model().objects.get(pk=user_id)
            user.last_online = timezone.now()  
            user.save(update_fields=['last_online'])
            return user
        except get_user_model().DoesNotExist:
            return None

Mistake
SystemCheckError: System check identified some issues:

ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'auth.User.groups' clashes with reverse accessor for 'users.User.groups'.
        HINT: Add or change a related_name argument to the definition for 'auth.User.groups' or 'users.User.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'auth.User.user_permissions' clashes with reverse accessor for 'users.User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'auth.User.user_permissions' or 'users.User.user_permissions'.
users.User.groups: (fields.E304) Reverse accessor for 'users.User.groups' clashes with reverse accessor for 'auth.User.groups'.
        HINT: Add or change a related_name argument to the definition for 'users.User.groups' or 'auth.User.groups'.
users.User.user_permissions: (fields.E304) Reverse accessor for 'users.User.user_permissions' clashes with reverse accessor for 'auth.User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'users.User.user_permissions' or 'auth.User.user_permissions'.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2022-04-15
@syschel

You are trying to override the user model, but you didn’t see in settings that there was an override. So you have a conflict with the base model, which is called exactly the same. AUTH_USER_MODEL
settings.py If there is a name conflict anyway, then rename your custom model, for example to MyUser
AUTH_USER_MODEL = 'my_auth_project.User'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question