简体   繁体   中英

How to cascade delete from a child to a parent

I have the following model:

class Todo(models.Model):
    user = models.OneToOneField(User)
    note = models.CharField(max_length=255)
    is_important = models.BooleanField(default=False)
    is_complete = models.BooleanField(default=False)
    reminder = models.OneToOneField(Reminder, blank=True, null=True, on_delete=models.SET_NULL)


class Reminder(models.Model):
    start_time = models.DateTimeField()
    stop_time = models.DateTimeField(blank=True)

Basically, a Todo becomes a Reminder when a start and optional end time are supplied.

At the moment, when I delete a Reminder object, the reminder field in the Todo object is set to Null, which what I want.

What I need to know:

How can I setup these models so that if a Todo object is deleted, the corresponding Reminder object will also be deleted?

Also, if it wasn't a one-to-one relationship, let's say it was a many-to-one (many Todo's to one Reminder) relationship, how could one setup the models so that if a Todo object was deleted, the Reminder object will also be deleted, but only if there were no more Todo objects linked to the Reminder?

Also, with regards to:

stop_time = models.DateTimeField(blank=True)

If it is left blank in the form, what will the default value be, stored in the database?

You are defining the relationship as

    reminder = models.OneToOneField(Reminder, blank=True, null=True,
                 on_delete=models.SET_NULL)

Here you are specifying on_delete=models.SET_NULL . This tells django to set the field to NULL when a referenced object is deleted. If you want to delete the object too use models.CASCADE . Django uses this as default value.

More reference here

stop_time = models.DateTimeField(blank=True)

When nothing specified this field will not be set and will be blank, there is no default value for this.


As mentioned in the comment, if you want to set the reminder field in the corresponding Todo object to NULL when Reminder object is deleted, then you can put the OneToOne relationship field in Reminder class instead of Todo class.

As its onetoone relationship, as per django ORM its identical when you put in either of classes. You will access it through a attribute from both objects.

For Many-to-one you will have to use pre-post delete signal. But signals have their own limitations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM