Django:我们如何在 Group 模型上添加额外的字段

问题描述 投票:0回答:3

嗨,我正在使用 Django 2+ 开发 django 权限系统。我想在 Django 的 Group 模型中添加额外的字段,但现在我不知道该怎么做。我尝试过类似的事情:

models.py
from django.contrib.auth.models import Group

class Group(models.Model):
    Group.add_to_class('description', models.CharField(max_length=180,null=True, blank=True))

但是当我迁移模型时,它会抛出错误:

Migrations for 'auth':
  /usr/local/lib/python3.6/site-packages/django/contrib/auth/migrations/0010_group_description.py
    - Add field description to group
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 184, in handle
    self.write_migration_files(changes)
  File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/makemigrations.py", line 223, in write_migration_files
    with open(writer.path, "w", encoding='utf-8') as fh:
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/site-packages/django/contrib/auth/migrations/0010_group_description.py'  
python django django-models django-permissions django-guardian
3个回答
4
投票

为了修改现有模型,您必须从该模型继承:

from django.contrib.auth.models import Group


class CustomGroup(Group):
    description = models.CharField(max_length=180,null=True, blank=True)
    

使用add_to_class意味着依赖未记录的 内部结构没有稳定性保证,并且是 如有更改,恕不另行通知。


1
投票

试试这个,(不在小组课上)

# models.py
from django.contrib.auth.models import Group

Group.add_to_class('description', models.CharField(max_length=180,null=True, blank=True))


0
投票

如果您已经使用自定义用户模型,那么有一种更简洁的方法可以实现此目的此处的文档

您可以使用 django-group-model 包用额外的字段替换您自己的 Group 模型。以下是有关如何执行此操作的简短摘要。请检查包文档,因为它详细介绍了该过程。

首先使用

pip install django-group-model
安装软件包。

使用附加字段创建自定义组模型。

from django_group_model.models import AbstractGroup


class Group(AbstractGroup):
    description = models.CharField(max_length=50, null=True, blank=True)

设置

AUTH_GROUP_MODEL
设置。

AUTH_GROUP_MODEL = 'myapp.Group' # Make sure to replace 'myapp' with the appropiate app

将组模型与您的用户模型一起使用


class User(AbstractUser, ...):
    ...
    groups = models.ManyToManyField(
        'myapp.Group',
        blank=True,
        related_name="user_set",
        related_query_name="user",
    )
    ...
© www.soinside.com 2019 - 2024. All rights reserved.