[Django]-Read only models and displayed as a list in django admin?

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

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

Leave a comment