2

Here are my models:

class Ride(models.Model):
    driver = models.ForeignKey('auth.User', related_name='rides_as_driver')
    destination = models.CharField(max_length=100, default='')
    leaving_time=models.TimeField()
    leaving_date=models.DateField(default=datetime.date.today)
    num_of_spots=models.IntegerField()
    passengers=models.ManyToManyField('auth.User', related_name="rides_as_passenger")
    mid_destinations = models.CharField(max_length=100, default='')


class PendingRequest(models.Model):
    driver = models.ForeignKey('auth.User', related_name='driver_pending_requests')
    passenger = models.ForeignKey('auth.User', related_name='passenger_pending_requests')
    ride = models.ForeignKey(Ride)

As you can see - I have a Ride model with ManyToMany field to auth.user (passengers), ForeginKey to auth.user (driver), and also a PendingRequest model which has a ForeginKey field to Ride.

Now, I want to to be able to delete a Ride object, have a custom code after the delete, and make sure that other models update accordingly.

For example, if I delete a Ride -> I want to have a custom code (send notification to passengers) -> and then delete the entries in the passenger table that have the deleted ride id in their ride field, and also the PendingRequest entries which have the deleted ride id.

How should I go about this?

Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101

1 Answers1

0

All you describe it business logic. You should have a function for that, override the destroy member - if you're using ViewSet - and call the business function from it.

Edit: corrected delete to destroy.

destroy signature is: destroy(self, request, *args, **kwargs):

Linovia
  • 19,812
  • 4
  • 47
  • 48
  • thanks. the delete method name to override is just 'def delete'? – Ofek Agmon Dec 04 '15 at 09:52
  • My bad, it's destroy. Editing the response. – Linovia Dec 04 '15 at 10:02
  • thanks. one weird thing that is happening is , without the overriding - when I do DELETE rides/139/ I get a Not Found 404 response. I check in the database and there is a ride with the id of 139. why is that? – Ofek Agmon Dec 04 '15 at 10:24