Thursday, July 30, 2015

Does imports of related models affect cascade deletion?

Hey,

I stumbled upon a piece of code and a comment that says this:

Deleting a model object that has related objects will only cascade delete those objects if their models have been imported.

Is this true? I have not found it in the documentation and would like to add a reference to the code comment so others won't be as confused as I am.

Here's an illustration (from memory, disregard if code is not valid python):

mymodel.py:

from django.db import models

class MyModel(models.Model):
    foo = models.CharField()
    bar = models.CharField()


my_other_model.py:

from django.db import models
from mymodel import MyModel

class MyOtherModel(models.Model):
    baz = models.CharField()
    bar = models.ForeignKey(MyModel)


some_source_file.py:

from mymodel import MyModel

# Without this line, deleting MyModel objects will not delete its related
# MyOtherModel objects (?):
from my_other_model import MyOtherModel

obj = MyModel.objects.get(pk=123)
obj.delete()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4991f097-ae07-4075-89c5-908df7b4ccf2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment