Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Proxy Model Permissions Do Not Appear

I extended Django admin site for my app to allow non-staff/superusers access. This is working just fine.

I created a proxy model for an existing model and registered it to my admin site, however, it doesn't appear for non-staff users. From the documentation I read, my understanding is that proxy models get their own permissions. I checked and these don't appear in the list of available permissions.

Here's my code in case it helps:

Normal Model

class Engagement(models.Model):
    eng_type = models.CharField(max_length=5)
    environment = models.CharField(max_length=8)    
    is_scoped = models.BooleanField()    

    class Meta:
        ordering = ['eng_type', 'environment']
        app_label = 'myapp'

Proxy Model

class NewRequests(Engagement):
    class Meta:
        proxy = True
        app_label = 'myapp'
        verbose_name = 'New Request'
        verbose_name_plural = 'New Requests'

Model Admin

class NewRequestsAdmin(ModelAdmin):
pass

def queryset(self, request):
    return self.model.objects.filter(is_scoped=0)

Custom Admin Registration

myapps_admin_site.register(NewRequests, NewRequestsAdmin)

I've been managing my DB with South. According to this post, you have to tamper with it a bit by following the instructions it points users to. This was a failure. My DB doesn't have a whole lot of info in it, so I uncommented South and ran a regular syncdb to rule out South. Unfortunately, this is still not working and I'm at a loss. Any help is appreciated.

Edit

This was on Django 1.4

like image 206
chirinosky Avatar asked Feb 23 '13 06:02

chirinosky


4 Answers

Turns out I didn't do anything wrong. I was looking for the permissions under

myapp | New Request | Can add new request

Permissions fall under the parent model.

myapp | engagement | Can add new request

like image 63
chirinosky Avatar answered Sep 21 '22 18:09

chirinosky


This is fixed in Django 2.2, quoting release notes:

Permissions for proxy models are now created using the content type of the proxy model rather than the content type of the concrete model. A migration will update existing permissions when you run migrate.

and docs:

Proxy models work exactly the same way as concrete models. Permissions are created using the own content type of the proxy model. Proxy models don’t inherit the permissions of the concrete model they subclass.

like image 44
mrts Avatar answered Sep 22 '22 18:09

mrts


There is a workaround, you can see it here: https://gist.github.com/magopian/7543724

It can vary based on your django version, but the priciple is the same.

Tested with Django 1.10.1

# -*- coding: utf-8 -*-

"""Add permissions for proxy model.
This is needed because of the bug https://code.djangoproject.com/ticket/11154
in Django (as of 1.6, it's not fixed).
When a permission is created for a proxy model, it actually creates if for it's
base model app_label (eg: for "article" instead of "about", for the About proxy
model).
What we need, however, is that the permission be created for the proxy model
itself, in order to have the proper entries displayed in the admin.
"""

from __future__ import unicode_literals, absolute_import, division

import sys

from django.contrib.auth.management import _get_all_permissions
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.core.management.base import BaseCommand
from django.apps import apps
from django.utils.encoding import smart_text

class Command(BaseCommand):
    help = "Fix permissions for proxy models."

    def handle(self, *args, **options):
        for model in apps.get_models():
            opts = model._meta
            ctype, created = ContentType.objects.get_or_create(
                app_label=opts.app_label,
                model=opts.object_name.lower(),
                defaults={'name': smart_text(opts.verbose_name_raw)})

            for codename, name in _get_all_permissions(opts):
                p, created = Permission.objects.get_or_create(
                    codename=codename,
                    content_type=ctype,
                    defaults={'name': name})
                if created:
                    sys.stdout.write('Adding permission {}\n'.format(p))

How to use

  • create a directory /myproject/myapp/management/commands
  • create the file /myproject/myapp/management/__init__.py
  • create the file /myproject/myapp/management/commands/__init__.py
  • save the code above into /myproject/myapp/management/commands/fix_permissions.py
  • run /manage.py fix_permissions
like image 36
Bruno João Avatar answered Sep 18 '22 18:09

Bruno João


This is a known bug in Django: https://code.djangoproject.com/ticket/11154 (check comments for some patches)

like image 39
Danny W. Adair Avatar answered Sep 22 '22 18:09

Danny W. Adair