自定义Django组模型 [英] Customize Django Group Model

查看:198
本文介绍了自定义Django组模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django管理员中添加一个字段给组,例如,当您在后端创建组时,您定义名称和权限,我想添加一个字段到该列表(CharField)。这是否需要一个新的应用程序,还是可以在我的根models.py中扩展组模型?

I'm trying to add a field to 'Groups' within the Django admin - for instance, when you create a group in the backend, you define 'Name' and 'Permissions', and I'd like to add a field to that list (CharField). Does this require a new app, or can I extend the Group model in my root models.py?

新来的django和python这里,对不起,如果这个问题的措辞不好。

New to django and python here, so sorry if the question is badly worded.

到目前为止:

#models.py

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

class AppDBName(Group):
    AppDB = models.CharField(max_length=30)
    def __unicode__(self):
        return self.AppDB

#admin.py

from django.contrib import admin
from .models import AppDBName   

admin.site.register(AppDBName)


推荐答案

您可以尝试创建一个新的模式,将扩展django内置的组模型。为此,您应该将一个新的模型(例如GroupExtend)与一个OneToOne字段链接到原来的模型,就像可以为用户( link ):

You can try to create a new model, which will extend the django built-in group model. To do this you should link a new model, say GroupExtend, to the original one with a OneToOne field, like it can be done for user (link):

from django.contrib.auth.models import Group

class GroupExtend(models.Model):
    group = models.OneToOneField(Group)
    # other fields

这篇关于自定义Django组模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆