Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: When to customize save vs using post-save signal

Tags:

I have a series of tests and cases in a database. Whenever a test is obsoleted, it gets end dated, and any sub-cases of that test should also be end dated. I see two ways to accomplish this:

1) Modify the save function to end date sub-cases.
2) Create a receiver which listens for Test models being saved, and then end dates their sub-cases.

Any reason to use one other than the other?

Edit: I see this blog post suggests to use the save method whenever you check given values of the model. Since I'm checking the end_date, maybe that suggests I should use a custom save?

Edit2: Also, for the record, the full hierarchy is Protocol -> Test -> Case -> Planned_Execution, and anytime one is end_dated, every child must also be endDated. I figure I'll end up doing basically the same thing for each.

Edit3: It turns out that in order to tell whether the current save() is the one that is endDating the Test, I need to have access to the old data and the new data, so I used a custom save. Here's what it looks like:

def save(self):     """Use a custom save to end date any subCases"""     try:         orig = Test.objects.get(id=self.id)         enddated = (not orig.end_date) and self.end_date is not None        except:         enddated = False      super(Test, self).save()      if enddated:         for case in self.case_set.exclude(end_date__isnull=False):             case.end_date = self.end_date             case.enddater = self.enddater             case.save() 
like image 868
Nathan Avatar asked Apr 08 '11 15:04

Nathan


People also ask

Should you use signals in Django?

Only use signals to avoid introducing circular dependencies. If you have two apps, and one app wants to trigger behaviour in an app it already knows about, don't use signals. The app should just import the function it needs and call it directly.

What is post save in Django?

Post-save SignalThe post_save logic is just a normal function, the receiver function, but it's connected to a sender, which is the Order model. The code block below demonstrates the sample receiver function as a post-save. 1from django. db. models.

What is pre save and post in Django?

You can use pre_save for example if you have a FileField or an ImageField and see if the file or the image really exists. You can use post_save when you have an UserProfile and you want to create a new one at the moment a new User it's created.

What is the use of Post_delete signal in Django?

Django Signals - post_delete()To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.


1 Answers

I generally use this rule of thumb:

  • If you have to modify data so that the save won't fail, then override save() (you don't really have another option). For example, in an app I'm working on, I have a model with a text field that has a list of choices. This interfaces with old code, and replaces an older model that had a similar text field, but with a different list of choices. The old code sometimes passes my model a choice from the older model, but there's a 1:1 mapping between choices, so in such a case I can modify the choice to the new one. Makes sense to do this in save().
  • Otherwise, if the save can proceed without intervention, I generally use a post-save signal.
like image 124
mipadi Avatar answered Sep 28 '22 15:09

mipadi