我希望为属于特定国家的实体提供超级用户访问权限。
例如:瑞典人的宿灿只管理瑞典的实体等。
然而,我是django的新手(接管旧系统),我需要一条生命线。
我希望能够指定一个关系表。
我已经添加了一个用户配置文件,并创建了一个名为super_user_country_link = models.ForeignKey(SuperUserToCountry,blank=True,null=True)的新字段
然后在一个新的类下面
class SuperUserToCountry(models.Model):
user = models.ForeignKey(User)
country = models.ForeignKey(Country) 我计划运行脚本,然后为每个超级用户添加一个条目,并为他们提供一个到国家0的链接(即,没有国家/地区=>总su访问权限)。然后,我可以在开始放入特定于国家的条目时删除这些条目,以便稍后我可以调用(以房屋为例):
if user.is_superuser:
if user.get_profile().super_user_county_link.country == 0:
#show house detail...
elsif user.get_profile().super_user_county_link.country == 0
#show house detail...
else
pass所以看一下它,这应该意味着我可以列出多个国家和单个用户,对吗?也许我想得太多了,但是这看起来正确吗?
我来自php的背景,所以我只是对这有多正确有点怀疑……
发布于 2010-08-21 01:43:15
如果我错了,请纠正我。在我看来,您试图在UserProfile和Country之间建立一种多对多的关系。如果是这样的话,最好的方法就是使用ManyToManyField。如下所示:
class UserProfile(models.Model):
countries = models.ManyToManyField(Country)只要您没有将任何其他信息作为此关系的一部分进行存储,您就可以在不需要单独的模型(SuperUserToCountry)的情况下将其保留。如果您确实计划存储其他信息,可以使用way for that too。
这里不需要条件blank = True和null = True。如果没有与用户配置文件关联的国家/地区,则国家/地区将返回空的lost (即,an_instance.countries.all()将为空)。
当您开始添加国家/地区时,您可以这样做:
profile = User.get_profile()
denmark = Country.objects.get(name = 'Denmark')
russia = Country.objects.get(name = 'Russia')
if denmark in profile.countries.all():
print "Something is rotten in the state of Denmark"
elsif russia in profile.countries.all():
print "In Soviet Russia, profiles have countries!"上述条件很可能会根据您的特定需求更精确地表达出来。顺便说一下,您将向用户的配置文件添加一个国家/地区,如下所示:
profile = User.get_profile()
denmark = Country.objects.get(name = 'Denmark')
profile.countries.add(denmark)https://stackoverflow.com/questions/3532574
复制相似问题