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
Ошибка
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'.