Python >> Tutoriel Python >  >> Python

Permission de voir, mais pas de changer ! -Django

Mettre à jour :Depuis Django 2.1, ceci est maintenant intégré.

Dans admin.py

# Main reusable Admin class for only viewing
class ViewAdmin(admin.ModelAdmin):

    """
    Custom made change_form template just for viewing purposes
    You need to copy this from /django/contrib/admin/templates/admin/change_form.html
    And then put that in your template folder that is specified in the 
    settings.TEMPLATE_DIR
    """
    change_form_template = 'view_form.html'

    # Remove the delete Admin Action for this Model
    actions = None

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False

    def save_model(self, request, obj, form, change):
        #Return nothing to make sure user can't update any data
        pass

# Example usage:
class SomeAdmin(ViewAdmin):
    # put your admin stuff here
    # or use pass

Dans change_form.html, remplacez ceci :

{{ adminform.form.non_field_errors }}

avec ceci :

<table>
{% for field in adminform.form %}
    <tr>
      <td>{{ field.label_tag }}:</td><td>{{ field.value }}</td>
    </tr>
{% endfor %}
</table>

Supprimez ensuite le bouton d'envoi en supprimant cette ligne :

{% submit_row %}

Vous pouvez utiliser l'application django-admin-view-permission :

pip install django-admin-view-permission

INSTALLED_APPS = [
    'admin_view_permission',
    'django.contrib.admin',
    ...
]

MISE À JOUR :

Django 2.1 dispose d'une autorisation d'affichage prête à l'emploi.


Vous ne pouvez pas simplement voir les choses dans l'administration Django.

Il existe une application de navigation de données pour cela.