Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/peopledoc/django-readonly-field
Make Django model fields readonly
https://github.com/peopledoc/django-readonly-field
approved-public ghec-mig-migrated peopledoc-opensource tribe-python
Last synced: 17 days ago
JSON representation
Make Django model fields readonly
- Host: GitHub
- URL: https://github.com/peopledoc/django-readonly-field
- Owner: peopledoc
- License: mit
- Created: 2016-09-02T12:34:56.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-01-27T14:37:47.000Z (almost 2 years ago)
- Last Synced: 2024-04-16T06:52:49.579Z (9 months ago)
- Topics: approved-public, ghec-mig-migrated, peopledoc-opensource, tribe-python
- Language: Python
- Homepage:
- Size: 117 KB
- Stars: 21
- Watchers: 12
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- Changelog: HISTORY.rst
- Contributing: CONTRIBUTING.rst
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
=============================
Django Readonly Field
=============================.. image:: https://badge.fury.io/py/django-readonly-field.png
:target: https://pypi.org/pypi/django-readonly-field.. image:: https://travis-ci.org/peopledoc/django-readonly-field.png?branch=master
:target: https://travis-ci.org/peopledoc/django-readonly-field.. image:: https://img.shields.io/codecov/c/github/peopledoc/django-readonly-field/master.svg
:target: https://codecov.io/github/peopledoc/django-readonly-field?branch=masterMake 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.11Documentation
-------------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`` :
.. code-block:: python
INSTALLED_APPS = [
# ...
"django_readonly_field",
]In the model where you want some fields to be read-only:
.. code-block:: python
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 /bin/activate
export DATABASE_URL=postgres://USER:PASSWORD@HOST:PORT/NAME
(myenv) $ pip install -r requirements.txtRun 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:
* Cookiecutter_
* `cookiecutter-djangopackage`_.. _Cookiecutter: https://github.com/audreyr/cookiecutter
.. _`cookiecutter-djangopackage`: https://github.com/pydanny/cookiecutter-djangopackage