Django, auto setting a field during a save, based on other Admin page inputs

隐身守侯 提交于 2019-12-01 13:58:40
Danny W. Adair

The problem is your m2m relationship with Suffix, or rather the way that django admin saves m2m relationships.

A pretty good explanation is in this answer to Why is adding site to an object doesn't seem to work in a save() override in the Django admin?

When you save a model via admin forms it's not an atomic transaction. The main object gets saved first (to make sure it has a PK), then the M2M is cleared and the new values set to whatever came out of the form.

post_save() is actually still too early. That's where the instance was saved, not its relationships.

You need to connect to the m2m_changed signal: https://docs.djangoproject.com/en/dev/ref/signals/#m2m-changed

or wait for Django 1.4 where ModelAdmin gives you a "when all is done" signal: https://code.djangoproject.com/ticket/16115

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!