https://github.com/dorosch/django-readonly-model
The easiest way to create read-only models for django
https://github.com/dorosch/django-readonly-model
django django-application django-readonly django-routing python readonly
Last synced: 2 months ago
JSON representation
The easiest way to create read-only models for django
- Host: GitHub
- URL: https://github.com/dorosch/django-readonly-model
- Owner: dorosch
- License: mit
- Created: 2020-04-04T13:44:18.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-29T10:00:29.000Z (almost 5 years ago)
- Last Synced: 2025-03-15T05:49:06.476Z (2 months ago)
- Topics: django, django-application, django-readonly, django-routing, python, readonly
- Language: Python
- Homepage:
- Size: 14.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# django-readonly-model
[](https://travis-ci.org/dorosch/django-readonly-model)
[](https://codecov.io/gh/dorosch/django-project-start)
[](https://badge.fury.io/py/django-readonly-model)
[](https://pepy.tech/project/django-readonly-model)The easiest way to create read-only models
## Installation
Install using `pip`:
pip install django-readonly-model
Add `'readonly_model'` to your `INSTALLED_APPS` setting:
INSTALLED_APPS = [
...
'readonly_model',
]## Example
Declare a model to read:
```python
from django.db import modelsclass Directory(models.Model):
class Meta:
read_only_model = True
```We can read data from the model but we cannot write:
```python
>>> from app.models import Directory
>>> Directory.objects.count()
0
>>> Directory.objects.create(name='kg')
...
readonly_model.exceptions.ReadOnlyModel: Model 'app.models.Directory' is read-only
```You cannot write but you can load data from fixtures:
```bash
$ python3 manage.py loaddata fixtures/directory.json
``````python
>>> from app.models import Directory
>>> Directory.objects.count()
3
```## When is it needed?
- 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.