The easiest way to create read-only models
Install using pip:
pip install django-readonly-model
Add 'readonly_model' to your INSTALLED_APPS setting:
INSTALLED_APPS = [
...
'readonly_model',
]
Declare a model to read:
from django.db import models
class Directory(models.Model):
class Meta:
read_only_model = TrueWe can read data from the model but we cannot write:
>>> from app.models import Directory
>>> Directory.objects.count()
0
>>> Directory.objects.create(name='kg')
...
readonly_model.exceptions.ReadOnlyModel: Model 'app.models.Directory' is read-onlyYou cannot write but you can load data from fixtures:
$ python3 manage.py loaddata fixtures/directory.json>>> from app.models import Directory
>>> Directory.objects.count()
3-
When you want to protect the model from accidental recording.
-
When you have some data that cannot be changed programmatically (for example, various directories).
-
When you need to use a read-only model from a database that you cannot write to it.