2013-11-15 53 views
4

我想添加两个组并为他们授予对我的Django项目的权限。但我不断收到错误:创建django权限,错误:ContentType匹配查询不存在

ContentType匹配查询不存在。

我运行: Django的1.5.4 的Python 2.7.3 南0.8.2 PostreSQL 9.3

这里是我的代码:

import django 
from django.contrib.auth.models import Group, Permission 
from django.contrib.contenttypes.models import ContentType 

from .models import Flavor 

def add_groups(): 
    # Create User Groups 
    special_users = Group(name='Special Users') 
    special_users.save() 
    really_special_users = Group(name='Super Special Users') 
    really_special_users.save() 

def add_permissions(): 
    # Define a View permission for the 1st group, and a View/Modify permission for the 2nd group 
    somemodel_ct = ContentType.objects.get(app_label='flavors', model='flavors_flavor') 
    can_view = Permission(name='Can View', codename='can_view_something', content_type=somemodel_ct) 
    can_view.save() 
    can_modify = Permission(name='Can Modify', codename='can_modify_something', content_type=somemodel_ct) 
    can_modify.save() 

def give_perm_to_groups(): 
    # Associate these two permissions now with a Group 
    special_users.permissions.add(can_view) 
    really_special_users.permissions = [can_view, can_modify] 

我可以运行add_groups()的罚款。这是现在正在工作的add_permissions()。我相信这与Postgres中的fixture有关,但不知道如何添加它们,或者如果这是确切的问题?

感谢

这里是整个错误回溯:

>>> add_permissions() 
Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/Users/Yuki_Aaron/Documents/djcode/demoproject/flavors/groups.py", line 16, in add_permissions 
    somemodel_ct = ContentType.objects.get(app_label='flavors', model='flavors_flavor') 
    File "/Users/Yuki_Aaron/Documents/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get 
    return self.get_query_set().get(*args, **kwargs) 
    File "/Users/Yuki_Aaron/Documents/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/query.py", line 404, in get 
    self.model._meta.object_name) 
DoesNotExist: ContentType matching query does not exist. 
+0

你叫“flavors_flavor”或“flavor”吗? – karthikr

回答

1

我必须做的第一件事就是改变somemodel_ct到:

somemodel_ct = ContentType.objects.get(app_label='flavors', model='flavor') 

的第二件事是,该错误是由于这样的事实,我需要权限添加到我的模型,为了django.contrib.auth.models ...... PermissionGroup能够查找模型中已定义的权限,然后再与ContentType联系起来。下面是我添加到我的models.py为了得到它的工作代码:

class Flavor(models.Model): 
... 
    class Meta: 
      permissions = (
       ('can_view', 'Can View'), 
       ('can_modify', 'Can Modify'), 
      ) 

这样,我Flavor模型可以查找权限在Permission表。这就是为什么它说ContentType: no matching query,因为我没有在我的实际Flavor模型中包括class Meta: permissions

感谢您的帮助!

2

改变这一行:

somemodel_ct = ContentType.objects.get(app_label='flavors', model='flavors_flavor') 

由:

somemodel_ct = ContentType.objects.get(app_label='flavors', model='flavor') 

这是当指定似乎有问题模型

+0

来自[django文档](https://docs.djangoproject.com/en/1.6/ref/contrib/contenttypes/#the-contenttype-model):** ContentType模型字段 - >模型类的名称** – juliocesar

+0

我将它改为“Flavor”,但它仍然无效。这是新的回溯。任何建议: –

+0

>>> add_permissions() 回溯(最近通话最后一个): 文件 “”,1号线,在 文件 “/Users/Yuki_Aaron/Documents/djcode/demoproject/flavors/groups.py” ,第16行,在add_permissions somemodel_ct = ContentType.objects.get(app_label ='flavors',model ='flavor') 文件“/Users/Yuki_Aaron/Documents/virtualenvs/django1.5/lib/python2.7/site -packages/django/db/models/manager.py“,第143行,在得到 返回self.get_query_set()。get(* args,** kwargs) 文件 DoesNotExist:ContentType匹配查询不存在。 –