Skip to main content

Make Django model fields readonly

Project description

Deployed to PyPI Python library GitHub Repository Continuous Integration Documentation Coverage MIT License

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 4.2 to 5.1

  • With Python, tested from 3.8 to 3.12

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

This repository was once available at peopledoc/django-readonly-field.

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.3.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

django_readonly_field-1.3.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file django_readonly_field-1.3.0.tar.gz.

File metadata

  • Download URL: django_readonly_field-1.3.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for django_readonly_field-1.3.0.tar.gz
Algorithm Hash digest
SHA256 f5ce043fa0e638a292dccceeec34be7fed103e455263544971332d14b3c3b31b
MD5 4ba4a8a7519ef7005393524aa89d8dec
BLAKE2b-256 bd0ca4f8cac2868e06c83ceb4e00e8e50df395596e794ac54678111eafa00d57

See more details on using hashes here.

File details

Details for the file django_readonly_field-1.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_readonly_field-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f83cc468937179d62a4162e39813dc453b34bea7028d12b68d01cf739f368df
MD5 7d802bfde245486ffe698e61786b298e
BLAKE2b-256 aaa9e0cec0fff38fe2be06d41a1cff70ea561deb83ec2af73b708522f071a32e

See more details on using hashes here.

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