views:

26

answers:

2

What I mean is I have:

class Client(models.Model):
    some_field = models.CharField()

class Ticket(models.Model):
    client = models.ForeignKey(Client)

Tickets are FOREVER in my system, but I want users to be able to delete clients they don't want anymore. Currently it'll delete all the tickets created by the Client.

  1. Is this a bad idea (architecturally speaking), and should I just mark them as not_needed or something instead?
  2. If it's not a bad idea, what's the best way to do it, while remaining DRY. I don't want to have to override delete() for each model that does this, but will if I have to (what's the best way to do that, if that's the only way).
A: 

Personally, I think it is a bad idea (you'd have orphan ticket records). I would just mark those clients as 'not_needed' or 'deleted'. You also get the added benefit of the ability to 'un-delete' those clients later.

sriehl
+2  A: 

The django.contrib.auth module has to deal with this same problem in the User model. Their solution is to have:

class User(models.Model):
    # ...
    is_active = models.BooleanField(default=True)
    # ...

So "deleting" a User is just setting is_active to False. Everything that works with Users needs to check is_active.

For the record, I think deleting Clients in this case is a Bad Idea.

But for the sake of argument, if you delete a Client, then its related Tickets need to first become clientless. Change the model for Ticket to:

class Ticket(models.Model):
    client = models.ForeignKey(Client, null=True, blank=True, 
        related_name='tickets')

Then, to delete a Client, do:

for ticket in clientToDelete.tickets:
    ticket.client = None
    ticket.save()
clientToDelete.delete()

You can put this code into Client's delete method, but it will get skipped if you do a mass (i.e. QuerySet-based) delete of Clients.

Mike DeSimone