hello I face this probleme when trying to connect to Django Admin here is my source code i do not want to change my legacy user table last_login field in date time it is an integer field that stop date time in unix timestamp . here is my source code
from django.db import models
from django.contrib.auth.models import AbstractBaseUser,BaseUserManager
from .helper import get_current_unix_timestamp
from django.contrib.auth.signals import user_logged_in
class MyUserManager(BaseUserManager):
def create_user(self, username,password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not username:
raise ValueError('user must have a username')
user = self.model(
username=username,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, password=None):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
user = self.create_user(
username,
password=password,
)
user.is_admin = True
user.is_superuser = True
user.is_staff = True
user.save(using=self._db)
return user
class Account(AbstractBaseUser):
id = models.AutoField(primary_key=True,verbose_name='ID',)
username = models.CharField(max_length=50, blank=True, null=True,unique=True)
password = models.CharField(max_length=255, blank=True, null=True)
email = models.CharField(max_length=255, blank=True, null=True)
ip = models.CharField(max_length=255,null=True,blank=True)
date_registered = models.IntegerField(default=get_current_unix_timestamp())
last_login = models.IntegerField(null=True,blank=True)
member_group_id = models.IntegerField(blank=True, null=True)
credits = models.FloatField(blank=True, null=True)
notes = models.TextField(blank=True, null=True)
status = models.IntegerField(blank=True, null=True)
reseller_dns = models.TextField(blank=True, null=True)
owner_id = models.IntegerField(blank=True, null=True)
override_packages = models.TextField(blank=True, null=True)
hue = models.CharField(max_length=50, blank=True, null=True)
theme = models.IntegerField(blank=True, null=True)
timezone = models.CharField(max_length=255, blank=True, null=True)
api_key = models.CharField(max_length=64, blank=True, null=True)
is_admin = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
objects = MyUserManager()
USERNAME_FIELD = 'username'
class Meta:
managed = True
db_table = 'users'
ordering = ['username']
def __str__(self):
return self.username
def update_last_login(sender, user, **kwargs):
"""
A signal receiver which updates the last_login date for
the user logging in.
"""
user.last_login = get_current_unix_timestamp()
user.save(update_fields=['last_login'])
user_logged_in.send(update_last_login)
the get_current_unix_timestamp function source code import datetime
def get_current_unix_timestamp():
"""this function retrun a unix format datetime"""
return int(datetime.datetime.now().strftime('%s'))
CodePudding user response:
Overriding update_last_login
will not work since it uses the one from the django.contrib.auth.models
module.
Perhaps it is better to make a subclass of the IntegerField
that automatically converts a datetime
field to an int
:
# app_name/fields.py
from django.db.models.fields import IntegerField
from time import mktime
class DateTimeIntegerField(IntegerField):
def get_prep_value(self, value):
if isinstance(value, date):
value = mktime(value.timetuple())
return super().get_prep_value(value)
def to_python(self, value):
if isinstance(value, date):
value = mktime(value.timetuple())
return super().to_python(value)
This field accepts both numbers and date
/datetime
objects. We can then use that field in the model:
from app_name.fields import DateTimeIntegerField
class Account(AbstractBaseUser):
# …,
last_login = DateTimeIntegerField(null=True,blank=True)
# …
This also simplifies the other logic, since now you can pass a datetime
to the field.