create django permissions, error: ContentType matc

2019-06-24 05:14发布

问题:

I am trying to add two groups and give them permissions to my Django project. But I keep getting the error:

ContentType matching query does not exist.

I am running: Django 1.5.4 Python 2.7.3 South 0.8.2 PostreSQL 9.3

Here is my code:

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]

I can run add_groups() fine. It is the add_permissions() that is now working. I believe this is related to fixtures in Postgres, but not sure how to add them or if that is the exact issue?

Thanks

Here is the whole error traceback:

>>> 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.

回答1:

Change this line:

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

by:

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

It's seem a problem when specify the model



回答2:

The first thing I had to do was to change the somemodel_ct to:

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

The second thing was that the error was due to the fact, that I needed to add permissions to my model, in order for the django.contrib.auth.models .... Permission and Group to be able to look up the defined permissions in the model, and then relate back to the ContentType. Here is the code that I added to my models.py in order to get it to work:

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

This way, my Flavor model can look up the permissions in the Permission table. That is why it said ContentType: no matching query because I hadn't included the class Meta: permissions in my actual Flavor model.

Thanks for your help!