Friday, December 19, 2014

Re: ModelAdmin with inlines: How to learn if/what has changed before the models are saved?

Hi Carsten,

save_model() happens first, then save_related() which calls save_formset() on each formset.

It might end up being easier to save the parent model _again_, instead of doing something before it's saved.

_has_changed() / has_changed() may come in handy in your case.
https://docs.djangoproject.com/en/dev/ref/forms/fields/#has-changed

Collin

On Thursday, December 18, 2014 12:20:34 PM UTC-5, Carsten Fuchs wrote:
Hi fellow Django developers,

using a model with several related models, for the Django Admin I have a
ModelAdmin that uses several InlineModelAdmin objects, very much as in
the example at
<https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#inlinemodeladmin-objects>.

All works well, but when the user clicks the "Save" button in the change
view, I would like to find out if anything has changed in the parent
model or the related (inline) models, and depending on the result,
update one of the parent model's fields before it is saved.

Can you please tell me how can this be achieved?


In more detail, having read all of
https://docs.djangoproject.com/en/1.7/ref/contrib/admin/ and thinking
that I have understood most of it, the ModelAdmin methods

     save_model()
     save_formset()
     save_related()

seem to be the right approach to my problem, but are a complete mystery
to me: How are the related to each other, and what is their purpose?

The documentation of all three begins with "The save_* method is given
the HttpRequest, ...", but what follows it too sparse for my still
limited Django knowledge, and unfortunately I cannot see yet the bigger
picture about them, or how to proceed from there.

Could someone please explain how these methods work, when they are
called, etc.?


A huge thanks in advance and best regards,
Carsten

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/98a9aac1-9174-47eb-9ac0-c8ea035bf279%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment