Scenario: there is different service types, i.e. clothes_washing, room_cleaning and room_maintenance. Each one of these services have common fields like service_date for example.
Upon scenario I have model for each service and a Service
model with common fields. the relation between Service
model and each service model is OneToOneField
.
I've tried to override the delete()
function in Service
model following this answer and It works for me but for only one service (like wheel in self.wheel.delete()
). but if I want to delete upon the service type? how to achieve that approach?
my models.py:
class ClothesWashing(models.Model):
# special fields for clothes_washing
service = models.OneToOneField(Service, on_delete=models.DO_NOTHING, null=True)
class RoomCleaning(models.Model):
# special fields for room_cleaning
service = models.OneToOneField(Service, on_delete=models.DO_NOTHING, null=True)
class Service(models.Model):
# common fields for all services
def delete(self, *args, **kwargs):
#here I wanna "roomcleaning" attribute to be dynamic upon content type
self.roomcleaning.delete()
return super(self.__class__, self).delete(*args, **kwargs)