Make Django model fields readonly
Project description
Make Django model fields readonly. In other words, make it so that Django will read from your fields in your database, but never try to write them. It can be useful if your fields are populated by triggers or something.
Requirements
Postgresql only
Django, obviously. v1.11+ (until proven otherwise)
Running under Python 2.7 or 3.5+
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 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:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-readonly-field-1.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d134d08e1cfb3f523ad5a001391541feff31112c1e4e538fd7707d3d8ae2263 |
|
MD5 | 70d5c7e79ea409e5ee4fc8e2625dead5 |
|
BLAKE2b-256 | 45ec983a525165d2f6fa361ed82c093e672dbb4af591c47cbde255fd46d3c026 |
Hashes for django_readonly_field-1.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9c721b5a6213ce0a57edba5679f93bea140388556d5bd467106f44906cf7f96 |
|
MD5 | 833868f7880e02ee5af0cc95f310c9da |
|
BLAKE2b-256 | f98bee66d815f6a8d161fd09e7b97d3d165fd371ebe76277dee39f8ae667b6c6 |