Edit-once object in admin?

4 views
Skip to first unread message

Roy Smith

unread,
Apr 28, 2012, 10:30:44 AM4/28/12
to django...@googlegroups.com
I've got a model that looks something like this:

class Image(Model):
    name = CharField(max_length=40)

class ImageVersion(Model):
    image = ForeignKey(Image)
    upload_date = DateTimeField(auto_now_add=True)
    data = ImageField(upload_to="images/learncms")

I want all the data in an ImageVersion to be read-only once it is created.  The idea is that the active version of an image is always the latest one, and the only way to change an image is to upload a new version.  Existing versions can never be changed.  How can I implement this in the admin?

Shawn Milochik

unread,
Apr 28, 2012, 10:35:40 AM4/28/12
to django...@googlegroups.com
I think you have to change your model.

Override the save() method to raise an exception if "self.id is not None."

This won't prevent developers from using the 'update()' or 'delete()'
methods of a queryset, but for a single instance it shouldn't be
editable. It will still be able to be deleted, unless you override
delete() as well.

Roy Smith

unread,
Apr 28, 2012, 11:07:59 AM4/28/12
to django...@googlegroups.com


On Saturday, April 28, 2012 10:35:40 AM UTC-4, Shawn Milochik wrote:
Override the save() method to raise an exception if "self.id is not None."

But, that still doesn't tell the admin interface to *show* the fields as read-only (which is really what I'm interested in).  If I raise an exception in save(), the admin interface will let somebody edit the fields and then give some sort of error when the click the Save button. 
Reply all
Reply to author
Forward
0 new messages