2

I want to display a model from db onto my django admin. What I want do with it is just simply display the details, no editing. is there a way to somehow "disable" the CRUD functions of a model in the django admin ? I can't seem to find a way. my current django version is 2.1.1. thanks for the help!

ronan
  • 369
  • 1
  • 4
  • 13
  • Yes You can do one thing, Every admin has ge_urls method, Override that method and Don't Call Super. Make your custom url and view that can display only readonly fields. – Parth Modi Sep 05 '18 at 05:30
  • how ? sorry i'm new to django. – ronan Sep 05 '18 at 05:42
  • 1
    forget that comment, just follow https://stackoverflow.com/questions/8265328/readonly-models-in-django-admin-interface] link. – Parth Modi Sep 05 '18 at 06:10

2 Answers2

3

You need to use read only fields.

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    ...
    fields = ['test-field-a']
    readonly_fields = fields

    def has_change_permission(self, request, obj=None):
        return False
jackotonye
  • 3,537
  • 23
  • 31
1

For read-only model you can use app django-readonly-model

Install using pip:

pip install django-readonly-model

Add 'readonly_model' to your INSTALLED_APPS setting:

INSTALLED_APPS = [
    ...
    'readonly_model',
]

And just use:

from django.db import models

class Directory(models.Model):
    class Meta:
        read_only_model = True

if you try to record something, you will get an exception:

>>> Directory.objects.create(name='kg')
...
readonly_model.exceptions.ReadOnlyModel: Model 'app.models.Directory' is read-only