Does imports of related models affect cascade deletion?

22 views
Skip to first unread message

Markus Amalthea Magnuson

unread,
Jul 30, 2015, 9:38:09 AM7/30/15
to Django users
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()

Tim Graham

unread,
Jul 30, 2015, 11:51:11 AM7/30/15
to Django users, markus....@gmail.com
No, import of related models shouldn't affect cascade deletion. Please open a bug report with steps to reproduce if that's actually the case. Possibly it could be a bug that's fixed by the app loading refactor in 1.7.

Marc Aymerich

unread,
Jul 30, 2015, 12:40:47 PM7/30/15
to django-users
On Thu, Jul 30, 2015 at 3:38 PM, Markus Amalthea Magnuson
<markus....@gmail.com> wrote:
> 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.

Is this models module called models.py and does it live inside a
Django application listed in INSTALLED_APPS?


--
Marc

Markus Amalthea Magnuson

unread,
Jul 30, 2015, 5:29:10 PM7/30/15
to django...@googlegroups.com
I will fetch the actual piece of code and build a small test case out of it, will reply again in this thread when I have some more info.


--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/wLiEuzqUVeQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@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/CA%2BDCN_tKE56qJPJ339uL0Zo%2BvEDSsPdTnLMODHC_3H4hd856dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages