Tuesday, September 25, 2012

Avoid Cascading-deletes in Django-Admin

If you try deleting an object referenced by a Foreign Key or OneToOneField in Django Admin, it will attempt to do a cascade delete on all the related objects. This is documented here.

To avoid this, you need to set null = True and on_delete=models.SET_NULL as attributes for the Foreign Key like so:
user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)

Ref: http://stackoverflow.com/a/6963556/1415352

No comments:

Post a Comment

Popular Posts