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?
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.)
精彩评论