# Perform action when Foreignkey is changing

**URL:** <https://forum.djangoproject.com/t/perform-action-when-foreignkey-is-changing/33860>\
**Category:** Using the ORM\
**Created:** [August 14, 2024, 11:56am UTC](https://forum.djangoproject.com/t/perform-action-when-foreignkey-is-changing/33860 "2024-08-14T11:56:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SousageMouse](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sousagemouse/32/23237_2.png) [@SousageMouse](https://forum.djangoproject.com/u/SousageMouse)\
**Post date:** [August 14, 2024, 11:56am UTC](https://forum.djangoproject.com/t/perform-action-when-foreignkey-is-changing/33860/1 "2024-08-14T11:56:43Z")

</div>

Hi everyone  
I have three models:

```auto
class Parent(models.Model):
  name = models.CharField(blank=True, max_length=20)
  ... # other fields

class OneChild(models.Model):
  name = models.CharField(blank=True, max_length=20)
  parent = models.ForeignKey(Parent, on_delete=models.SET_NULL, blank=True, null=True, related_name='one_childs')`
  ... # other fields

class TwoChild(models.Model):
  name = models.CharField(blank=True, max_length=20)
  parent = models.ForeignKey(Parent, on_delete=models.SET_NULL, blank=True, null=True, related_name='two_childs')`
  ... # other fields

```

Is there any options to perform some action while saving parent model instance with changed foreignkey values for childs? For example, write to file new names of childs?  
Now i try to use post\_save signal for Parent, but i got current names instead of new.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 14, 2024, 12:33pm UTC](https://forum.djangoproject.com/t/perform-action-when-foreignkey-is-changing/33860/2 "2024-08-14T12:33:59Z")

</div>

Welcome @SousageMouse !

You don’t need to use signals, or anything like that.

You could extend the `Parent.save()` method and have it call an update method (when necessary) on the child models. (Then, you only need to ensure that all updates to `Parent` use that save method.)

If you need to ensure that this will _always_ be done under _all_ circumstances, then you would need to write a trigger on the database to force the update to occur at the database layer.

---

<div class="post-metadata">

**Author:** ![SousageMouse](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sousagemouse/32/23237_2.png) [@SousageMouse](https://forum.djangoproject.com/u/SousageMouse)\
**Post date:** [August 14, 2024, 1:00pm UTC](https://forum.djangoproject.com/t/perform-action-when-foreignkey-is-changing/33860/3 "2024-08-14T13:00:44Z")

</div>

Many thanks for your reply.  
Will it work if the child models is changed, not parent?  
Maybe i dont get your answer

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 14, 2024, 1:41pm UTC](https://forum.djangoproject.com/t/perform-action-when-foreignkey-is-changing/33860/4 "2024-08-14T13:41:23Z")

</div>

I was addressing your question:

> [@SousageMouse](#):
>
> Is there any options to perform some action while saving parent model instance …

If there are more situations, such as saving one of the `Child` objects and not the `Parent`, then yes, there are other things that would need to be done. If you want to change the `Parent` when a `Child` changes, then that would be something added to the `Child.save()` method.

In that case, we would need a more detailed explanation of your set of requirements here.

What is everything that you are looking to do here?

---

<div class="post-metadata">

**Author:** ![white-seolpyo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/white-seolpyo/32/22386_2.png) [@white-seolpyo](https://forum.djangoproject.com/u/white-seolpyo)\
**Post date:** [August 15, 2024, 12:09am UTC](https://forum.djangoproject.com/t/perform-action-when-foreignkey-is-changing/33860/5 "2024-08-15T00:09:44Z")

</div>

A child holds only the parent’s primary key value.  
Therefore, unless the parent’s pk is changed or deleted, changing the contents of each model will not affect it.

---

<div class="post-metadata">

**Author:** ![SousageMouse](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/sousagemouse/32/23237_2.png) [@SousageMouse](https://forum.djangoproject.com/u/SousageMouse)\
**Post date:** [August 15, 2024, 8:24am UTC](https://forum.djangoproject.com/t/perform-action-when-foreignkey-is-changing/33860/6 "2024-08-15T08:24:37Z")

</div>

The main task is to create a json with the values of the primary model, including values from the linked models, when saving the main model or the linked models. The problem is that when a link is updated, the json does not contain the new link, but the old one (or none if there was none). For the sake of experiment I changed the link to m2m and using m2m\_changed got the current links. The question is whether this can be achieved on fk and how

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [August 15, 2024, 3:15pm UTC](https://forum.djangoproject.com/t/perform-action-when-foreignkey-is-changing/33860/7 "2024-08-15T15:15:24Z")

</div>

I’m sorry but this description is still too vague. I don’t follow what the sequence of events is supposed to be, what the data is being used, and what your ultimate objective is.

For me to try and help you here, you are going to need to provide a detailed and specific example of exactly what it is you’re trying to do. Please post the actual code involved and sample data showing the issue.
