Skip to main content

Make Django model fields readonly

Project description

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

Make some Django model fields readonly. In other words, it lets you tell Django to read some fields from your database, but never try to write those back. It can be useful if your fields are populated by triggers or something.

Requirements

  • Postgresql only

  • Django, tested from 2.2 to 4.1

  • With Python, tested from 3.7 to 3.11

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 an IntegrityError.

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

Running Tests

You will need a usable Postgresql database in order to test the project.

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

Run tests for a specific version

(myenv) $ pytest

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

$ tox

Credits

Tools used in rendering this package:

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-1.1.2.tar.gz (21.0 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-1.1.2-py2.py3-none-any.whl (11.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: django-readonly-field-1.1.2.tar.gz
  • Upload date:
  • Size: 21.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for django-readonly-field-1.1.2.tar.gz
Algorithm Hash digest
SHA256 cc22ec25fbead0cc33753b876d2632c84633f7f42df7510d448f7245d8878675
MD5 159e600004337e0480b37b49456650ba
BLAKE2b-256 92417f0742d7c5b1b871cd4eddb8abce313bb6c0d5377105d274866eaaafb4ff

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_readonly_field-1.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5e7e9f492ea88390a92dfb6a3a3c7ead456b9536e3e8f7bd1c6482eaeadf568f
MD5 230b9b06556b425edeb45c56f50537c1
BLAKE2b-256 7282a626b805498850e5a6f1075b9d91c712481a97a4f2d52812cafc81853470

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