0

I have a django model which contains details of when a particular task (email reminder) should be run for a particular booking.

It looks a bit like this:-

class Task(models.Model):
    PENDING = 'pending'
    IN_PROGRESS = 'in_progress'
    COMPLETE = 'complete'
    STATES = (
        (PENDING, 'Pending'),
        (IN_PROGRESS, 'In Progress'),
        (COMPLETE, 'Complete')
    )

    run_at = models.DateTimeField()
    state = models.CharField(max_length=50, choices=STATES, default=PENDING)

    class Meta:
        ordering = ['run_at']

When the date of the booking is changed, I want the corresponding task's run_at time to be updated.

As far as I can see, there are a few ways I can do this:-

  1. In the view code.
  2. In the save method of the Task model.
  3. Using signals.

In general, I think it makes most sense to use the view code - it's more explicit that way. But the code I'm working with is very old and the date of bookings can be changed from all sorts of places in all sorts of ways and I'm not convinced I can be 100% certain that I've caught every single place in which this can happen. If I miss any, the Task won't be updated in that case.

Plus, if anyone else adds code that changes booking dates, they'll have to know to update the tasks.

So would it make more sense to use the save method? Or just try and be as thorough as I can and find all the places that the booking date could be altered?

bodger
  • 1,112
  • 6
  • 24

1 Answers1

0

Signals are designed for such tasks, and in your case I believe using the signals is the best way to go.

Using the view code, as you suggested, brings the problems of having to update code in several places, and future developers also having to keep those constraints in mind when adding code that changes booking dates. So it is not a good solution for this problem.

Overriding the save method of the Task model is also a viable solution, but as signals are designed for such tasks, it would be a better option code-style wise.

Ozgur Akcali
  • 5,264
  • 2
  • 31
  • 49