1

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?

1 Answer 1

2

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

5
  • I do not want to use to models.CASCADE because when a Reminder object is deleted I want to set the reminder field in the corresponding Todo object to NULL, but when a Todo object is deleted I want to delete the corresponding Reminder object. How do I setup my models to achieve this? Aug 18, 2014 at 16:31
  • @OliverFunk, In that case you can set the onetoone relationship in Reminder class. For Many-to-one you will have to use pre-post delete signal. But signals have their own limitations.
    – Rohan
    Aug 19, 2014 at 4:24
  • But that seems like an unnecessary field in the Reminder object. I mean, the relationship is defined in the Todo object already, why must I define it in the Reminder object too? That will also create another column in the database aswell, it just seems unnecessary.. Should I create a method inside the Todo object that executes when a Todo is deleted and that deletes the Reminder object associated with it? Aug 22, 2014 at 16:53
  • @OliverFunk, I suggested to move relationship field to Reminder class.
    – Rohan
    Aug 23, 2014 at 6:22
  • Ah damn! I misread your comment... Your suggestion makes complete sense. Can you change your answer so that I can mark it correct? Aug 23, 2014 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.