Make Django model fields readonly
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.
- Postgresql only
- Django, obviously. v1.11+ (until proven otherwise)
- Running under Python 2.7 or 3.5+
The full documentation is at https://django-readonly-field.readthedocs.org.
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.
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.
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)
Using the project
Many operations are documented in the Makefile. For more information, use:
$ make help
- Run tests for Django 2.2 and Python 3.5, 3.6 and 3.7
- Drop support of Python 3.4
- Confirm support of Python 3.7
- Drop support of Django 1.8
- Drop support of Django 1.9
- Drop support of Django 1.10
- Confirm support of Django 2.0
- Confirm support of Django 2.1
- Python 3.6 support
- Multiple databases support
- Github organization changed to “peopledoc”
- Added tests for Django 1.11
- Added support for Python 3.4 and current Django stable
- Fix a thread bug that would make the package usable only for test and not for real :/
- First stable version
- Rationalized the writing of readonly (vs read-only)
- Defined default app config
- CI improvements
- Code linting
- First release on PyPI.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size django_readonly_field-1.0.5-py2.py3-none-any.whl (6.0 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View|
|Filename, size django-readonly-field-1.0.5.tar.gz (7.1 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for django_readonly_field-1.0.5-py2.py3-none-any.whl
Hashes for django-readonly-field-1.0.5.tar.gz