gpt4 book ai didi

python - Django 自定义默认组模型

转载 作者:太空宇宙 更新时间:2023-11-03 13:59:10 25 4
gpt4 key购买 nike

我对 Django 相当陌生,我想根据自己的要求自定义 Django 的默认 Group 模型,

默认 django 的 Group 模型如下所示:

class Group(models.Model):
name = models.CharField(_('name'), max_length=80, unique=True)
permissions = models.ManyToManyField(
Permission,
verbose_name=_('permissions'),
blank=True,
)

我想添加一个新字段,并将名称字段和该新字段作为一个复合主键。

有办法做到这一点吗?

如有任何建议,我们将不胜感激。谢谢

最佳答案

这是解决您的问题的方法:

from django.contrib.auth.models import Group


# inherit from Group instead of copy the code...
class Group(Group):
new_field = = models.CharField(
verbose_name=('new-field'),
max_length=31,
)

class Meta:
indexes = [
models.Index(fields=['last_name', 'first_name'], name='mixed_idx'),
]

Read more关于indexs选项。 (别名index_together)。

关于python - Django 自定义默认组模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49381367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com