如何扩展Django Group并仍然在Django User中使用 [英] How to extend Django Group and still use in Django User
问题描述
我正在寻求扩展Django组的功能,所以我可以在Group上添加额外的属性,例如集团主页的url。这样的东西:
类组织(组):
url = models.CharField(max_length = 100)
然而,当使用此组织并将其添加到用户(通过使用 org.user_set .add(user)
)我没有办法从用户访问url字段。当我执行 user.groups.all()
时,显示用户位于组织(而不是组织)中,与我在组织上设置的相同名称有机
。那么我如何添加功能到组,但是可以从我的用户信息中保存?
你有两个选项; p>
1)新模型;
class GroupProfile(models.Model)
group = models.OneToOneField('auth.Group',unique = True)
url = models.CharField(max_length = 100)
2)Monkey Patch;
Group.add_to_class('url' models.CharField(max_length = 100))
在第二个选项中,您必须使用像 south
用于数据库迁移。
I am looking to extend the functionality of Django's group, so I can have extra properties on the Group such as a url for the Group's homepage. Something like this:
class Organization(Group):
url = models.CharField(max_length=100)
However when using this organization and adding it to a user (by using org.user_set.add(user)
) I have no way of accessing the url field from the User. When I do user.groups.all()
it shows the user is in a Group (not Organization) with the same name I set on my organization org
. So how I do I add functionality to Group but keep it accessible from my user info?
You have two option;
1) New model;
class GroupProfile(models.Model):
group = models.OneToOneField('auth.Group', unique=True)
url = models.CharField(max_length=100)
2) Monkey Patch;
Group.add_to_class('url', models.CharField(max_length=100))
In second option you have to use app like south
for db migration.
这篇关于如何扩展Django Group并仍然在Django User中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!