How do you prevent Django's cascading delete on a User object?
When a User (auth.user) is deleted, I'd like to prevent the cascading delete from clearing certain tables. How can I prevent Django's User mode开发者_如何学Cl from executing the cascading delete?
Option 1:
Ensure all the objects that link to the target object you want to delete support null=True
For the object you want to delete, iterate through all of reverse relations (ie, the ones that point back to the object you're deleting) and set them to to
None
, remembering to saveDelete your target object.
Option 2:
- Use Django 1.3 -> http://docs.djangoproject.com/en/dev/releases/1.3-alpha-1/#configurable-delete-cascade
Use newly released django 1.3a1
http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.on_delete
精彩评论