Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write-once fields in Django models

I'm having a pretty hard time trying to create a write-once field in a Django model. Ideally I'd want it to work like a final variable, although I can settle for simply preventing it from being edited through the admin.

I know there is a solution for read-only fields, but it also affects the add form, and I don't want the field to be read-only there.

like image 555
Tomek Kaftal Avatar asked Sep 22 '10 09:09

Tomek Kaftal


1 Answers

Use get_readonly_fields(), and return a tuple with the write-once field name if obj exists, or an empty tuple if obj is None.

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_readonly_fields

like image 152
arantius Avatar answered Oct 05 '22 13:10

arantius