Dizzley
Dizzley

Reputation: 487

Django - Avoid saving unchanged object in admin

My admin.py seems to work very well thank you, but my user has the option of clicking "Save" even when nothing has changed. This sets the object's modified_date field which annoys me.

Is there a way to force a cancel when using "Save" in these circumstances to close a change form in the admin? Is it desirable to do so?

Upvotes: 1

Views: 695

Answers (1)

Steve Jalim
Steve Jalim

Reputation: 12195

You could probably write a custom admin form and subclass it for every admin-editable entity where, in the save() it looks at all fields which aren't the last_modified_date (I'm assuming you've got this consistently named across your models) and if there are no changes, doesn't call super(YourAdminFormClassNamehere, self).save(*args, **kwargs) but if there are changes to any of those fields, it does.

(It's a weekend, else I'd probably add some example code. This should get you on a useful track, though.)

Upvotes: 2

Related Questions