Explicitly allow to keep alive objects after deleting related ForeignKey object instance
|Reported by:||Alexey Kinyov||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.2-beta|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Sometimes relation between objects needs to be 'weak': on deleting 'parent' object we should define some 'fallback' keeping 'children' instanses alive.
It would look like this:
class MyModel(Model): user = ForeignKey(User, blank=True, null=True, weak=True)
def get_or_create_default_user(instance): # some project specific logic here to define # default user for MyModel instace # ... return user class MyModel(Model): user = ForeignKey(User, default=get_or_create_default_user, weak=True)
This will help prevent tricks with signal 'pre_delete' #6870.
Suggested processing of 'weak' relation: just before deleting 'parent' all related 'weak' relations must be set to default. If 'parent' is the same as default, then deleting of related objects will occur anyway. If not, related objects will survive :)