Skip to main content

Make Django model fields readonly

Project description

https://badge.fury.io/py/django-readonly-field.png https://travis-ci.org/novafloss/django-readonly-field.png?branch=master https://img.shields.io/codecov/c/github/novafloss/django-readonly-field/master.svg

Make Django model fields readonly. In other words, make it so that Django will read from your field in your Database, but never try to write it. It can be useful if your field is populated by a trigger or something.

Requirements

  • Postgresql only

  • Django, obviously

Documentation

The full documentation is at https://django-readonly-field.readthedocs.org.

Quickstart

Install Django Readonly Field:

pip install django-readonly-field

In your settings.py :

INSTALLED_APPS = [
    # ...
    "django_readonly_field",
]

In the model where you want some fields to be read-only:

class Spaceship(models.Model):
    name = models.CharField(max_length=100)
    color = models.CharField(max_length=16)

    class ReadOnlyMeta:
        readonly = ["color"]

That’s it. Now, Django won’t try to write the color field on the database.

Warning

Django won’t try to write those fields. Consequence is that your Database must be ok with Django not writing those fields. They should either be nullable or have a database default or be filled by a trigger, otherwise you will get in IntegrityError.

Don’t forget that Django model field defaults don’t become Database default. You might have to write an SQL migration for this.

Running Tests

You will need an usable Postgresql database in ordre to test the project.

source <YOURVIRTUALENV>/bin/activate
export DATABASE_URL=postgres://USER:PASSWORD@HOST:PORT/NAME
(myenv) $ pip install -r requirements_test.txt

Run tests for a specific version

(myenv) $ python runtests.py

Run tests for all versions (if tox is installed globally, you don’t need a virtual environment)

$ tox

Using the project

Many operations are documented in the Makefile. For more information, use:

$ make help

Credits

Tools used in rendering this package:

History

0.1.1 (2016-09-13)

  • CI improvements

  • Code linting

0.1.0 (2016-09-02)

  • First release on PyPI.

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-field-0.1.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_readonly_field-0.1.1-py2.py3-none-any.whl (6.2 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-readonly-field-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-readonly-field-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f2d2f9d609127417e0dfbb35ca9daddd6ceb2b285b2065468c78568e5e9f457a
MD5 2d0ff7b9dc7463910c64812df54d2cb1
BLAKE2b-256 fedad9dff7df22123c5305f7690825b92ee5589838488e4ea0371c0b1891655d

See more details on using hashes here.

File details

Details for the file django_readonly_field-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_readonly_field-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d09e2f09dab2fec63bc4bd859ffb2f12dad9539e4b0e9d5abcd0213a27a78420
MD5 cdcc99d5fcdfa76c8a6c2c4caf331989
BLAKE2b-256 81dbd9c5c24a2d79ce434ce3c83e663c0979df31a198af9256a768296c5cbfa4

See more details on using hashes here.

Supported by

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