Try using clear() prior to deleting:
http://docs.djangoproject.com/en/dev/topics/db/queries/#following-relationships-backward
--
---
David Zhou
da...@nodnod.net
Another way is to consider using a many to many there, rather than a
one to many.
Funny that you ask this question, because I just spent two days undoing
the Django cascading delete on my end. If you look at the delete()
method in the Django code, it only makes three calls. The first two
gather a list of all the objects that Django wants to delete.
Basically, I wrote a new delete() method that iterates through those
objects, finds all the references to the object that you want to delete
and sets them to null. It then saves them. After this, you need to
run those first two lines again to regather the objects you want to
delete (this time, only the one object should appear), and then you can
finally delete them.
I would argue this is more desireable than using clear() explicitly,
because you don't have to know anything about your models with this
method. Any time you have to remember to set a certain
relationship to null, you are bound to forget about another
relationship between your objects, and you'll get cascading delete on
something you didn't expect, and you won't have even noticed that it
happened!
--
Randy Barlow
http://electronsweatshop.com
But that just means you'll need to explicitly set cascade on those
models you *do* want to cascade delete. And, IMO, the vast majority
of foreign key use cases do benefit from an auto cascading delete.
For the specific Links/Groups example, personally I'd just do it with
a many to many relation. It's entirely feasible that in the future,
Links will need the ability to associate itself to multiple groups.
For now, I'd just limit a link to one group via some custom
validation.