Django管理员不允许我在实现AbstractUser后更改任何用户设置

d4so4syb  于 2022-12-05  发布在  Go
关注(0)|答案(1)|浏览(143)

以下是Youtube上的一个视频,我想创建一个网站,为学生和教师提供不同的角色。我还想保存用户的学校ID。
这是我想更改的内容,但当我点击保存时,它什么也没有做。

我试过创建自定义表单来进行创建和更改,但似乎并不重要。我觉得我在www.example.com中遗漏了一些东西model.py或者我遗漏了一些东西。创建用户就像一种魅力,所以这就是为什么我不明白为什么它不能正确地更新用户。我以前做过一个完全自定义的用户类,但应该不需要为了这个简单的更改。
在设定中是有这条线的

AUTH_USER_MODEL = "account_app.User"

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser, BaseUserManager
from django.db.models.signals import post_save
from django.dispatch import receiver

class School(models.Model):
    name = models.CharField('School Name', max_length=240)
    address1 = models.CharField('Address 1', max_length=240)
    address2 = models.CharField('Address 2', max_length=240)
    city = models.CharField('City', max_length=240)
    district = models.CharField('District', max_length=240)
    state = models.CharField('State', max_length=2)
    zip_code = models.IntegerField('Zip Code')
    country =models.CharField('Country', max_length=240)
    phone = models.IntegerField('School Phone Number')
    time_zone = models.CharField(max_length=50)

    def __str__(self):
        return f'{self.name}'

class User(AbstractUser):
    class Role(models.TextChoices):
        ADMIN = "ADMIN", "Admin"
        STUDENT = "STUDENT", "Student"
        TEACHER = "TEACHER", "Teacher"

    base_role = Role.ADMIN

    role = models.CharField(max_length=50, choices=Role.choices)
    school_id = models.ForeignKey(School,blank=True, null=True, on_delete=models.DO_NOTHING)

    def save(self, *args, **kwargs):
        if not self.pk:
            self.role = self.base_role
            return super().save(*args, **kwargs)

class StudentManager(BaseUserManager):
    def get_queryset(self, *args, **kwargs):
        results = super().get_queryset(*args, **kwargs)
        return results.filter(role=User.Role.STUDENT)

class Student(User):

    base_role = User.Role.STUDENT

    student = StudentManager()

    class Meta:
        proxy = True

    def save(self, *args, **kwargs):
        if not self.pk:
            self.role = User.role.STUDENT
            return super().save(*args, **kwargs)

    def welcome(self):
        return "Only for students"

class TeacherManager(BaseUserManager):
    def get_queryset(self, *args, **kwargs):
        results = super().get_queryset(*args, **kwargs)
        return results.filter(role=User.Role.TEACHER)

class Teacher(User):

    base_role = User.Role.TEACHER

    teacher = TeacherManager()

    class Meta:
        proxy = True

    def save(self, *args, **kwargs):
        if not self.pk:
            self.role = User.role.TEACHER
            return super().save(*args, **kwargs)

    def welcome(self):
        return "Only for teachers"

这里是admin.py

from django.contrib import admin
from .models import User
from .models import Student
from .models import Teacher

admin.site.register(User)
admin.site.register(Teacher)
admin.site.register(Student)
b4lqfgs4

b4lqfgs41#

您似乎更改了抽象用户类的默认权限,因此您可以尝试使用此方法为每个模型添加自定义权限,例如:

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

@admin.register(User)
class CustomUserAdmin(UserAdmin):
    def has_change_permission(self, request, obj=None):
        return True
    def has_delete_permission(self, request, obj=None):
        return False

以下是完整文档:管理面板权限文档

相关问题