The truth is rarely pure and never simple

django admin: post save hook for foreign keys with inline forms

The post_save hook from django.db.models.signals is executed right after the parent object has been stored in the database. As there is no hook for creation of updates of dependent objects, you have several alternatives:

  1. Attach to the post_save hook of your dependent model. This may work for all cases, where you do not have to react if no dependent object is created. However, even if this would be applicable to your situation, this will imply a serious overhead as you have to check whether the parent object has been updated recently.
  2. Override response_add of your ModelAdmin class. This is not very elegant but should work.
  3. Finally, you could override save_related of your ModelAdmin class. This is a quick solution if you know what to do:
def save_related(self, request, form, formsets, change):
  # here is the place for pre_save actions - nothing has been written to the database, yet
  super(type(self), self).save_related(request, form, formsets, change)
  # now you have all objects in the database
  if not change:
    # something new

Leave a comment

Your email address will not be published.