我制作了一个简单的Django应用程序。我有一个模型“访客”。我的目标是在Django管理员中出现两个两个表。一个与所有访客一起,一个只有今天的访客。
按照these instructions的说法,我完成了以下代码。但是我的麻烦是,在管理页面中编辑组时,我无法让VisitorExpectedTodayProxy显示在“可用权限”中。有谁知道怎么做?
Models.py
class Visitor(models.Model):
visit_datetime = models.DateTimeField(null=True)
visitor_name = models.CharField(max_length=500)
#Make dummy models for different object views in admin interface
class VisitorExpectedTodayProxy(Visitor):
class Meta:
proxy=True
verbose_name = "Visitor"
verbose_name_plural = "Today's Visitors and Regular Visitors"
更新
我确实运行了syncdb,但我仍然没有在管理网站上看到它。 syncdb的结果:
$ python manage.py syncdb
Syncing...
No fixtures found.
Synced:
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.messages
> django.contrib.admin
> south
答案 0 :(得分:1)
这是我用来手动输入代理对象的ContentType条目的脚本,然后为它们创建新的权限。
这只是生成sql,然后你应该在mysql中运行。之后,它们应显示在用户的权限列表中,并且需要添加它们。这些的一些命名约定并不明显。
your_models=['proxy model',
]
for model in models:
model_nospace = model.replace(' ','')
sql = 'insert into django_content_type (name, app_label, model) values ("%s","<<app_name>>","%s");'%(model,model_nospace)
print sql
for kind, permname in [('Can add','add_%s'%model_nospace),
('Can change','change_%s'%model_nospace),
('Can delete','delete_%s'%model_nospace),]:
sql = 'insert into auth_permission (name, content_type_id, codename) values ("%s %s",(select id from django_content_type where name="%s"),"%s");'% (kind,model,model,permname)
print sql
答案 1 :(得分:1)
我意识到这个问题已经暂时关闭了,但是我会分享对我有用的东西,以防它可以帮助别人。
事实证明,即使我创建的代理模型的权限列在syncdb
之后的父应用下,即使我授予非超级用户所有权限,但仍然拒绝访问我的代理模特通过管理员。
虽然我还没有尝试过基于SQL的修复程序,但修复Django级别的bug对我有用。您必须解决已知的Django错误(https://code.djangoproject.com/ticket/11154)并连接到post_syncdb
信号以正确创建代理模型的权限。以下代码根据该主题的一些评论从https://djangosnippets.org/snippets/2677/修改。
我把它放在myapp / models.py中,它包含我的代理模型。从理论上讲,这可以存放在INSTALLED_APPS
之后的任何django.contrib.contenttypes
中,因为需要在update_contenttypes
处理程序注册post_syncdb
信号后加载,因此我们可以将其断开连接。< / p>
def create_proxy_permissions(app, created_models, verbosity, **kwargs):
"""
Creates permissions for proxy models which are not created automatically
by 'django.contrib.auth.management.create_permissions'.
See https://code.djangoproject.com/ticket/11154
Source: https://djangosnippets.org/snippets/2677/
Since we can't rely on 'get_for_model' we must fallback to
'get_by_natural_key'. However, this method doesn't automatically create
missing 'ContentType' so we must ensure all the models' 'ContentType's are
created before running this method. We do so by un-registering the
'update_contenttypes' 'post_syncdb' signal and calling it in here just
before doing everything.
"""
update_contenttypes(app, created_models, verbosity, **kwargs)
app_models = models.get_models(app)
# The permissions we're looking for as (content_type, (codename, name))
searched_perms = list()
# The codenames and ctypes that should exist.
ctypes = set()
for model in app_models:
opts = model._meta
if opts.proxy:
# Can't use 'get_for_model' here since it doesn't return
# the correct 'ContentType' for proxy models.
# See https://code.djangoproject.com/ticket/17648
app_label, model = opts.app_label, opts.object_name.lower()
ctype = ContentType.objects.get_by_natural_key(app_label, model)
ctypes.add(ctype)
for perm in _get_all_permissions(opts, ctype):
searched_perms.append((ctype, perm))
# Find all the Permissions that have a content_type for a model we're
# looking for. We don't need to check for codenames since we already have
# a list of the ones we're going to create.
all_perms = set(Permission.objects.filter(
content_type__in=ctypes,
).values_list(
"content_type", "codename"
))
objs = [
Permission(codename=codename, name=name, content_type=ctype)
for ctype, (codename, name) in searched_perms
if (ctype.pk, codename) not in all_perms
]
Permission.objects.bulk_create(objs)
if verbosity >= 2:
for obj in objs:
sys.stdout.write("Adding permission '%s'" % obj)
models.signals.post_syncdb.connect(create_proxy_permissions)
# See 'create_proxy_permissions' docstring to understand why we un-register
# this signal handler.
models.signals.post_syncdb.disconnect(update_contenttypes)
答案 2 :(得分:0)
./manage.py syncdb
多数民众赞成。它将条目添加到auth_permission表中。
答案 3 :(得分:0)
我做的是解决这个问题,我删除了每个表(仅删除auth表是不够的)。然后我从settings.py中已安装的应用程序向南注释(不确定是否会导致问题)。
然后我运行了manage.py syncdb,现在一切正常。
我只需要重新加载我的所有数据。我还不确定它是如何搞砸的。
答案 4 :(得分:-1)