2013-02-23 15 views
7

मैंने गैर-कर्मचारी/सुपरसर्स एक्सेस की अनुमति देने के लिए मेरे ऐप के लिए Django व्यवस्थापक साइट को बढ़ाया। यह ठीक काम कर रहा है।Django प्रॉक्सी मॉडल अनुमतियां दिखाई नहीं दे रही हैं

मैंने एक मौजूदा मॉडल के लिए प्रॉक्सी मॉडल बनाया और इसे मेरे व्यवस्थापक साइट पर पंजीकृत किया, हालांकि, यह गैर-कर्मचारियों के उपयोगकर्ताओं के लिए नहीं दिखता है। प्रलेखन से मैंने पढ़ा है, मेरी समझ यह है कि प्रॉक्सी मॉडल को अपनी अनुमति मिलती है। मैंने चेक किया और ये उपलब्ध अनुमतियों की सूची में प्रकट नहीं होते हैं।

यहाँ यह मदद करता है के मामले में मेरी कोड है:

सामान्य मॉडल

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' 

प्रॉक्सी मॉडल

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

मॉडल व्यवस्थापक

class NewRequestsAdmin(ModelAdmin): 
pass 

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

कस्टम व्यवस्थापक पंजीकरण

myapps_admin_site.register(NewRequests, NewRequestsAdmin) 

मैं दक्षिण के साथ मेरी डीबी प्रबंध किया गया है। this post के अनुसार, आपको the instructions it points users to का पालन करके इसे थोड़ा सा छेड़छाड़ करना होगा। यह एक विफलता थी। मेरे डीबी में इसमें बहुत सारी जानकारी नहीं है, इसलिए मैंने दक्षिण को अपूर्ण कर दिया और दक्षिण में शासन करने के लिए एक नियमित सिंकडब चलाया। दुर्भाग्यवश, यह अभी भी काम नहीं कर रहा है और मुझे नुकसान हुआ है। किसी भी मदद की सराहना की है।

संपादित

यह

उत्तर

7

Django 1.4 पर था बाहर कर देता है मैंने कुछ गलत नहीं किया। मैं नीचे

myapp | New Request | Can add new request

अनुमतियां माता पिता मॉडल के अंतर्गत आते हैं अनुमतियों के लिए देख रहा था।

myapp | engagement | Can add new request

+0

मुझे यह समस्या भी थी। –

+0

क्या आप django का संस्करण उपयोग कर रहे हैं? मेरे पास 1.4 में एक ही सेटअप है और इसे हल नहीं किया गया है, इसलिए मैं सोच रहा हूं कि इसे हल किया गया था? – fastmultiplication

+0

यह 1.4 का भी उपयोग कर रहा था। सुनिश्चित नहीं है कि यह आपके लिए क्यों हल नहीं किया गया है। मैंने मूल पोस्ट को संस्करण के साथ अपडेट किया। – chirinosky

1

मुझे पता है इस सवाल का कुछ समय पहले बंद हो गया है, लेकिन मैं साझा कर रहा क्या मामले में मेरे लिए काम किया यह दूसरों की मदद कर सकता है।

यह पता चला है कि मेरे द्वारा बनाए गए प्रॉक्सी मॉडल के लिए अनुमतियां मूल ऐप्स (जैसे @chirinosky) के तहत सूचीबद्ध की गई हैं, और मैंने अपने गैर-सुपर उपयोगकर्ता को सभी अनुमतियां दी हैं, फिर भी यह मेरे लिए पहुंच से इनकार कर दिया गया था व्यवस्थापक के माध्यम से प्रॉक्सी मॉडल।

मुझे जो करना था वह एक ज्ञात Django बग (https://code.djangoproject.com/ticket/11154) का कामकाज था और प्रॉक्सी मॉडल के लिए उचित रूप से अनुमतियां बनाने के लिए post_syncdb सिग्नल से कनेक्ट किया गया था। नीचे दिए गए कोड को उस थ्रेड पर कुछ टिप्पणियों के अनुसार https://djangosnippets.org/snippets/2677/ से संशोधित किया गया है।

मैंने इसे myapp/models.py में रखा जो मेरे प्रॉक्सी मॉडल रखे।सैद्धांतिक रूप से यह django.contrib.contenttypes के बाद आपके INSTALLED_APPS में से किसी एक में रह सकता है क्योंकि update_contenttypes हैंडलर post_syncdb सिग्नल के लिए पंजीकृत है, इसलिए हम इसे डिस्कनेक्ट कर सकते हैं।

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) 
3

एक समाधान नहीं है, तो आप इसे यहाँ देख सकते हैं: https://gist.github.com/magopian/7543724

यह अपने Django संस्करण के आधार पर भिन्न हो सकते हैं, लेकिन सिद्धांत एक ही है।

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

साथ परीक्षण किया गया का उपयोग कैसे करें

  • एक निर्देशिका /myproject/myapp/management/commands
  • बनाने फ़ाइल /myproject/myapp/management/__init__.py
  • फ़ाइल /myproject/myapp/management/commands/__init__.py
  • कोड को बचाने बनाते हैंमें ऊपर 210
  • रन /manage.py fix_permissions
+0

संभावित समाधान का एक लिंक हमेशा स्वागत है, लेकिन कृपया [लिंक के चारों ओर संदर्भ जोड़ें] (http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links- कहीं- वास्तव में -गुड-उत्तर/825 9 # 825 9) इसलिए आपके साथी उपयोगकर्ताओं को कुछ पता चल जाएगा कि यह क्या है और यह क्यों है। यदि लक्षित साइट पहुंच योग्य नहीं है या स्थायी रूप से ऑफलाइन हो जाती है, तो हमेशा एक महत्वपूर्ण लिंक के सबसे प्रासंगिक भाग को उद्धृत करें। ध्यान रखें कि किसी बाहरी साइट के लिंक से मुश्किल से अधिक होने का एक संभावित कारण है [क्यों और कुछ उत्तरों कैसे हटाए जाते हैं?] (Http://stackoverflow.com/help/deleted-answers)। – FelixSFD

+0

निश्चित रूप से। मैं अपना जवाब सुधारूंगा। धन्यवाद! – vinagreti

+0

@FelixSFD आपकी मदद के लिए धन्यवाद। अब बहुत बेहतर और सहायक है। – vinagreti

संबंधित मुद्दे

 संबंधित मुद्दे