Skip to main content

Make Django model fields readonly

Project description

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.


  • Postgresql only

  • Django, tested from 2.2 to 4.1

  • With Python, tested from 3.7 to 3.11


The full documentation is at


Install Django Readonly Field:

pip install django-readonly-field

In your :

    # ...

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.

Running Tests

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

source <YOURVIRTUALENV>/bin/activate
(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


Tools used in rendering this package:

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 hashes)

Uploaded source

Built Distribution

django_readonly_field-1.1.2-py2.py3-none-any.whl (11.8 kB view hashes)

Uploaded py2 py3

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