I have two models:
class Client(models.Model):
some_field = models.CharField()
class Ticket(models.Model):
client = models.ForeignKey(Client)
Ticket
s 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
.
not_needed
or something instead?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).So this question is very old but in case someone runs across it (like I did): starting from Django 1.3, you can use the on_delete
parameter for ForeignKey
models, as described here.
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 User
s needs to check is_active
.
For the record, I think deleting Client
s in this case is a Bad Idea.
But for the sake of argument, if you delete a Client
, then its related Ticket
s 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 Client
s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With