Prefetch object with to_attr set to the same name as the field triggers rows deletions

Skip to first unread message


Nov 5, 2015, 9:04:31 AM11/5/15
to Django users
I'm using a Prefetch object to follow some GenericRelation, and I was playing around with the to_attr parameter;
I had the DB debug turned on, as I was checking whether it was making any difference, and I started noticing some DELETE statements on the remote table, so I started investigating.
It looks like those deletions are triggered within the prefetch process when the to_attr is set to the same name as the field; also, it does not seem to happen if the relationship is not a generic one.
While this usage is presumably wrong, it is not mentioned anywhere in the docs; also, I think that deleting rows from the database is a somewhat extreme way to educate users :)
Can someone enlighten me on what's going on here?

Thank you very much.

Simon Charette

Nov 5, 2015, 10:14:06 AM11/5/15
to Django users
Hi mccc,

Would it be possible to provide an example of the models and actions required to trigger the deletion.

I suppose the deletion is triggered by the many to many relationship assignment logic and we should simply raise a ValueError if `to_attr` is set the same name as the referred relationship.


Michele Ciccozzi

Nov 5, 2015, 11:17:04 AM11/5/15
Hello Simon,

I'm going to paste a trimmed-down version of the involved classes:

class CeleryTask(models.Model):
    celery_task_id = models.CharField(max_length=50, primary_key=True)
    task_type = models.ForeignKey(ContentType)
    task_id = models.PositiveIntegerField()
    task = GenericForeignKey('task_type', 'task_id')

class AccountManagementTask(models.Model):
    account = models.ForeignKey(Account, null=True, editable=False)
    task_content_type = models.ForeignKey(ContentType)
    task_content_id = models.PositiveIntegerField()
    task_content = GenericForeignKey('task_content_type', 'task_content_id')
    celery_task = GenericRelation(CeleryTask, content_type_field='task_type', object_id_field='task_id',

(the task_content thing is involved in a follow-up question that will be asked in a short while)

This is the code triggering the weirdness:

        previous_tasks = AccountManagementTask.objects.filter(account=account)\

Immediately after that, the QuerySet is sent off to a simple DRF serializer - which doesn't even touch the issue since the relevant piece of information is annotated:

class AccountManagementTaskSerializer(serializers.ModelSerializer):
    def to_representation(self, instance):
        return {'id':,
                'created_on': instance.created_on,
                'date_from': instance.task_content.date_from,
                'date_to': instance.task_content.date_to}

    class Meta:
        model = AccountManagementTask

Now the follow-up, that you might already have guessed: if I *do* use the same name for following the GenericForeignKey relation (task_content), the content of the relationship does not get deleted(no surprise there..?) and I can go with my naïve serialization over there, while if I specify a different to_attr name I get back a list and I have to deal differently with that.
How do these prefetch_related / Prefetch to_attr / Generic* things really work?

Merci beaucoup,

You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit
To unsubscribe from this group and all its topics, send an email to
To post to this group, send email to
Visit this group at
To view this discussion on the web visit

For more options, visit

Simon Charette

Nov 5, 2015, 6:00:11 PM11/5/15
to Django users
Bonsoir Michele,

I'll try to explain what happens here with a simplified model definition assuming you are using Django 1.8. I used a GenericRelation like your provided example but this issue can be triggered for about any M2M relationship.

Given the following models:

from django.db import models
from django.contrib.contenttypes.fields import (
GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType

class TaggedItem(models.Model):
= models.SlugField()
= models.ForeignKey(ContentType)
= models.PositiveIntegerField()
= GenericForeignKey('content_type', 'object_id')

class Bookmark(models.Model):
= models.URLField()
= GenericRelation(TaggedItem)

Assigning directly to a Bookmark instance tags attribute (a ReverseGenericRelatedObjectsDescriptor instance) will result in a delete query followed by multiple insert or update queries depending on whether they exist or not. For example:

>>> b = Bookmark.objects.create(url='')
>>> # The following line will result in the following queries:
>>> # 1) DELETE FROM bookmark WHERE id =;
>>> # 2) INSERT INTO taggeditem ...;
>>> # 3) INSERT INTO taggeditem ...;
>>> b.tags = [TaggedItem(tag='Python'), TaggedItem(tag='Web')]

Note that the DELETE query is not run as of 1.9 since Django now performs updates in 'bulk' instead (that is comparing the existing relationship with this specified value and only perform the required insert/update).

Now, as documented, when you specify a `to_attr` Django simply uses `setattr()` onder de motorkap and ends up triggering the code path described above if the specified attribute happens to be a reverse relationship descriptor. Note that is behavior will be deprecated in Django 1.10 and removed in Django 2.0.

I have to say I'm unsure how we should proceed here. I guess I would favor raising a ValueError if the model the list of prefetched objects will be attached to has a reverse descriptor for the specified `to_attr` at least until this behavior is deprecated since it looks like a major footgun to me.

I guess we should move the discussion to the development mailing list to gather feedback.

Au plaisir,

Michele Ciccozzi

Nov 6, 2015, 5:01:35 AM11/6/15
Thank you very much, Simon, for the detailed answer.
Now I'm digging through Django code, pull requests, and tickets, and it's all very interesting.
Thank you also for the reminder of incoming deprecations: we'll definitely be on the lookout for that!

All the best,

Simon Charette

Nov 7, 2015, 5:35:57 AM11/7/15
to Django users
Hi Michele,

I don't if someone beat you to it but a ticket for this exact issue just surfaced[1].

Please chime in on Trac if you'd like to follow the progress of the resolution.



Michele Ciccozzi

Nov 9, 2015, 4:28:03 AM11/9/15
Hello Simon,

Thank you very much for this very interesting follow-up: it's like everyday there's a new reason to keep digging in the source code!
I'll see you there ;)

All the best,

Reply all
Reply to author
0 new messages