views:

509

answers:

1

I'd like to remove some object with many-to-many relationship using Django admin interface. Standard removing also removes all related objects and the list of removed objects displayed on confirmation page. But I don't need to remove related objects!

Assume we have ContentTopic and ContentItem:

class ContentTopic(models.Model):
    name = models.CharField()
    code = models.CharField()

class ContentItem(models.Model):
    topic = models.ManyToManyField(ContentTopic, db_index=True,\
    blank=True, related_name='content_item')

So, I'd like to remove ContentTopic instance using Django admin, but I don't need remove all related ContentItems. So, confirmation page should display only ContentTopic instance to remove.

What is the best way to handle this?

A: 

This happens so, coz its developed to do so. If you want to change this behaviour, the one way can be over-riding delete method of django.db.models.Model.

This delete() method actually does two things, first gathering a list of all dependent objects and delete them. So here, you can override it, to get that list of dependent objects, iterating over it and set their reference to None, instead of deleting them. And thus deleting the concerned object cleanly.

May be if you want this behavior throughout, you can extend a class from django.db.models.Models, override delete(), and extend all your models from this new class.

simplyharsh