Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#12899 closed (duplicate)

Explicitly allow to keep alive objects after deleting related ForeignKey object instance

Reported by: rudi Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2-beta
Severity: Keywords: foreignkey, relations
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

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)

or

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 :)

Attachments (0)

Change History (2)

comment:1 Changed 4 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #7539

comment:2 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.