Django 管理:从自定义用户模型中删除记录

4

我创建了一个自定义用户模型(users_user),可以用于注册新用户(创建新的自定义用户记录)和登录。但是,如果我转到管理页面并尝试从那里删除用户记录,似乎是尝试从默认用户模型(auth_user)中删除记录。我收到以下错误:

IntegrityError at /admin/users/user/
insert or update on table "django_admin_log" violates foreign key constraint "django_admin_log_user_id_c564eba6_fk_auth_user_id"
DETAIL:  Key (user_id)=(3) is not present in table "auth_user".

有没有一种方法可以保留标准的Django管理页面(如下图所示),但引用我的自定义用户模型而不是标准的auth_user模型?

users/models.py

from django.db import models
from PIL import Image
from django.conf import settings
from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)

#~~~ CUSTOM USER ~~~

class UserManager(BaseUserManager):
    def create_user(self, email, username, password=None):
        print('username in UserManager.create_user(): ' + username)

        if not email:
            raise ValueError('Users must have an email address')

        if not username:
            raise ValueError('Users must have a username')

        user = self.model(
            email=self.normalize_email(email),
            username=username, #todo: confirm, is this right?
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_staffuser(self, email, password):
        """
        Creates and saves a staff user with the given email and password.
        """
        user = self.create_user(
            email,
            username=username,
            password=password,
        )
        user.staff = True
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password):
        print('username in UserManager.create_superuser(): ' + username)
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(
            email,
            username=username,
            password=password,
        )
        user.staff = True
        user.admin = True
        user.save(using=self._db)
        return user




class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    username = models.CharField(max_length=100, unique=True)
    is_active = models.BooleanField(default=True)
    staff = models.BooleanField(default=False) # a admin user; non super-user
    admin = models.BooleanField(default=False) # a superuser

    # notice the absence of a "Password field", that is built in.

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email'] # Email & Password are required by default.

    objects = UserManager()


    def get_short_username(self):
        # The user is identified by their email address
        return self.username[0:10] # Get first 10 chars of username

    def __str__(self):
        return self.username

    def has_perm(self, perm, obj=None):
        "Does the user have a specific permission?"
        # Simplest possible answer: Yes, always
        return True

    def has_module_perms(self, app_label):
        "Does the user have permissions to view the app `app_label`?"
        # Simplest possible answer: Yes, always
        return True

    @property
    def is_staff(self):
        "Is the user a member of staff?"
        return self.staff

    @property
    def is_admin(self):
        "Is the user a admin member?"
        return self.admin






class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_pics')

    def __str__(self):
        return f'{self.user.username} Profile'

    # def save(self, *args, **kwargs):
    #     super().save(*args, **kwargs)

    #     img = Image.open(self.image.path)

    #     if img.height > 300 or img.width > 300:
    #         output_size = (300, 300)
    #         img.thumbnail(output_size)
    #         img.save(self.image.path)

users/admin.py:

from django.contrib import admin
from .models import Profile

from django.contrib.auth import get_user_model
User = get_user_model()

admin.site.register(Profile)
admin.site.register(User) #<- THIS IS REGISTERING THE WRONG USER MODEL

enter image description here


2
你是否已经设置了你的 AUTH_USER_MODEL - Brian Destura
1
此外,关于Django管理日志的错误可能是另一个问题。请查看这里 - Brian Destura
请查看这个链接,对我很有用。以后再感谢我吧。 - Kenneth mwangi
2个回答

0

我在一个Django应用程序中拥有类似的抽象用户模型。这是我的admin.py设置,按照您的要求工作。

我已修改您的admin.py使其更像我的。我相信使用'get_user_model'函数时会导致问题。

另外,除非您有特定原因添加电子邮件、用户名、是否活跃和员工字段,否则子类化抽象用户模型已经提供了所有默认字段。我添加了一个额外的字段来将用户与配置文件类型模型外键关联。我使用UserAdmin模型和字段集在Django管理员中显示我的自定义用户字段。

from django.contrib import admin

# Import your abstract user model from the apps models.py
from .models import Profile, User

# Import the auth UserAdmin model
from django.contrib.auth.admin import UserAdmin


# I use this to add the custom fields the django admin form, you may not need it. 
fields = list(UserAdmin.fieldsets)
fields[0] = (None, {'fields': ('username','password','account')})
UserAdmin.fieldsets = tuple(fields)

admin.site.register(Profile)
# Register the two models
admin.site.register(User, UserAdmin) 

作为参考,这是我的 user model 的 models.py 文件的样子。

class User(AbstractUser):
account = models.ForeignKey('Account', on_delete=models.PROTECT, null=True, blank=True, unique=True)

class Meta:
    permissions = (
                   ("make_admin", "Can view and edit most admin features."),
                   ("edit_permissions", "Admin user can modify user permissions."),
                   ("edit_nacha", "User can edit and modify NACHA files."),
                   ("edit_commissions", "User can override commisions."),
                   ("view_reports", "User can view admin reports."),
                  )

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接