The django.contrib.auth module must deal with the same problem in the User model. Their solution is to:
class User(models.Model):
So the โremovalโ of a User simply sets is_active to False . Everything that works with User needs to check is_active .
For the record, I think that uninstalling Client in this case is a Bad idea .
But for the sake of argument, if you delete Client , then the associated Ticket must first become without a client. Change the model for Ticket to:
class Ticket(models.Model): client = models.ForeignKey(Client, null=True, blank=True, related_name='tickets')
Then, to remove a Client , do:
for ticket in clientToDelete.tickets: ticket.client = None ticket.save() clientToDelete.delete()
You can put this code in the Client delete method, but it will be skipped if you delete the array (i.e. based on QuerySet) of Client s.
source share