1

This question aims for best practises. So I have a model A and model B. model B has model A as foreign key but whenever a new instance of model A is created a new instance of model B has to be created and linked with that instance of model A.

Currently I have resolved the circular dependecy by referencing all foreign key models with strings and I override the save function of model A to check if a link to model B is already present and if not it creates one. But I'm not sure if this is the best thing to do.

Should I use a signal instead, so that whenever model A is saved a signal is received by post_save which checks if that instance already has a model B instance linked to it and if not creates it?

My only concern with signals is that they seem to take some time and I have to be sure that model B instance is created immediately.

knbk
  • 52,111
  • 9
  • 124
  • 122
Donna
  • 657
  • 1
  • 8
  • 18
  • 1
    Signals don't take any more time than anything else. – Daniel Roseman Nov 29 '15 at 16:05
  • Thank you @DanielRoseman maybe I was doing something wrong the last time I implemented stuff with signals. If this is the case I will go with the signal method since it's a bit more clear and cleaner – Donna Nov 29 '15 at 16:10
  • 1
    There's no right answer to this, only opinions. It's mostly a matter of code organization, rather than technical differences, and people just can't seem to agree. – knbk Nov 29 '15 at 17:44
  • Does this answer your question? [Django: When to customize save vs using post-save signal](https://stackoverflow.com/questions/5597378/django-when-to-customize-save-vs-using-post-save-signal) – ldrg Jan 07 '22 at 18:40

0 Answers0