Skip to main content

The easiest way to create read-only models for django

Project description

django-readonly-model

Build Status codecov PyPI version Downloads

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:

from django.db import models

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

We 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-only

You 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 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-readonly-model-1.2.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

django_readonly_model-1.2-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page