在Django中,我有一个模型Artists和一个带有UserManager
类的自定义User模型:
class UserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
"""Creates and saves a new user"""
if not email:
raise ValueError("Users must have an email address")
user = self.model(email=self.normalize_email(email), **extra_fields)
user.set_password(password)
user.save(using=self._db) # required for supporting multiple databases
return user
def create_superuser(self, email, password):
"""Creates and saves a new superuser"""
user = self.create_user(email, password)
user.is_staff = True
user.is_superuser = True
user.save(using=self._db)
return user
class User(AbstractBaseUser, PermissionsMixin):
"""Custom user model that supports using email instead of username"""
email = models.EmailField(max_length=255, unique=True)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = "email"
@property
def full_name(self):
return f'{self.first_name} {self.last_name}'
我希望超级用户,员工用户和艺术家自己能够编辑艺术家模型。最好的方法是什么?
我正在考虑像这样向我的艺术家模型添加一个字段:
users_who_can_edit = models.ManyToManyField(user=get_user_model())
但是我想我在视图中实现了过滤器?
答案 0 :(得分:0)
这种方法听起来像是可行的,并且它允许艺术家与多个用户关联,反之亦然。
您打算使用内置的Django admin进行编辑,还是构建自己的编辑视图?那会影响如何做的细节吗?