我正在努力让基于角色的权限适用于GAE的django-nonrel.
开箱即用,它似乎没有用,可能是因为用户和组之间存在隐含的多对多关系,所以我发现并安装了http://www.fhahn.com/writing/Django-s-权限 - 系统与Django-Nonrel.根据文档,我将permission_backend_nonrel添加到INSTALLED_APPS(在djangotoolbox之后),并将AUTHENTICATION_BACKENDS定义到settings.py中的相应类.
这让我超越了早期的问题("DatabaseError:数据库不支持此查询."),但我仍然卡住,因为当我运行一个非常简单的示例时,我得到一组空的权限,当我相信我应该得到回报.以下是我能做的一个简单的例子.它是由python manage.py shell在django框架中启动的 - 它是一个简单的小马店.我正在尝试将用户添加到组,授予该组权限,然后将这些权限反映为用户拥有的权限集的一部分:
>>> from django.contrib.auth.models import Group, Permission, User
>>> from django.contrib.contenttypes.models import ContentType
>>> from pony_shop.models import Pony
#Create the group:
>>> farmers = Group(name="Farmers")
>>> farmers.save()
>>> pony_ct = ContentType.objects.get(app_label='pony_shop', model='pony')
#Create the Permission
>>> can_twirl = Permission(name='Can Twirl', codename='can_twirl', content_type=pony_ct)
>>> can_twirl.save()
#Give the Permission to the Group
>>> farmers.permissions.add(can_twirl)
>>> farmers.save()
#Create the User
>>> francis = User(username='francis')
>>> francis.save()
#Put the user in the group
>>> …Run Code Online (Sandbox Code Playgroud) 我有DataFile模型,它们有LogOutput对象.一个数据文件对象将有一个LogOutput.对于属于DataFile的任何LogOutput,它只属于单个DataFile.其他模型也有LogOutput对象.
因为它们是一对一的,除了LogOutputs可以属于DataFiles以外的东西(例如,套件也有它们 - 参见下面的代码)我认为正确的做法是在DataFile中定义一个OneToOneField ,即该LogOutput.
models.py:
class LogOutput(models.Model):
raw = models.TextField()
class DataFile(models.Model):
name = models.CharField()#etc.
logoutput = models.OneToOneField(LogOutput)
class Suite(models.Model):
# Many Suites will have the same datafile:
datafile = models.ForeignKey(DataFile)
# Each Suite has a unique LogOutput different from the DataFile's
# and as with the DataFile, that LogOutput will have …Run Code Online (Sandbox Code Playgroud)