﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33008	prefetch_related() for deleted GenericForeignKey is not consistent.	Martin Svoboda	Martin Svoboda	"prefetch_related called for GenericForeignKey sets content_type_id and object_id to None, if the foreign object doesn't exist. This behaviour is not documented.

GenericForignKey is often used for audit records, so it can keep links to non-existing objects. Probably prefetch_related shouldn't touch original values of object_id and content_type_id and only set content_object to None.


{{{
from django.contrib.auth.models import User
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models


class TaggedItem(models.Model):
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')

# init data
guido = User.objects.create(username='Guido')
t = TaggedItem(content_object=guido, tag='test')
t.save()
guido.delete()

# get content_object normally
tags_1 = TaggedItem.objects.filter(tag='test')
tags_1[0].content_object  # returns None
tags_1[0].object_id  # returns 1
tags_1[0].content_type_id  # returns X

# use prefetch_related
tags_2 = TaggedItem.objects.filter(tag='test').prefetch_related(""content_object"")
tags_2[0].content_object  # returns None
tags_2[0].object_id  # returns None
tags_2[0].content_type_id # returns None
}}}
"	Bug	closed	contrib.contenttypes	3.2	Normal	fixed		Simon Charette Mariusz Felisiak	Ready for checkin	1	0	0	0	0	0
