#34198: Django content type prefetching wrong data
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
MahavirAce |
Type: Bug | Status: new
Component: Database | Version: 4.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{{
class Base1(models.Model):
value = models.CharField(max_length=20)
class Base2(models.Model):
value = models.CharField(max_length=20)
class LinkedWithBase1(models.Model):
f = models.ForeignKey(Foreign, on_delete=models.PROTECT,
related_name="a_objects")
class LinkedWithBase2(models.Model):
f = models.ForeignKey(OtherForeign, on_delete=models.PROTECT,
related_name="b_objects")
class Connection(models.Model):
ct = models.ForeignKey(ContentType, on_delete=models.PROTECT)
object_id = models.IntegerField()
object = GenericForeignKey(ct_field='ct', fk_field='object_id')
}}}
**The data presentation :-**
1) Base1 Model
|| id || value||
|| 1|| Foreign||
2) Base2 Model
|| id || value||
|| 1|| OtherForeign||
3) LinkedWithBase1 model
|| id || f_id (FK)||
|| 1|| 1||
4) LinkedWithBase2 model
|| id || f_id (FK)||
|| 1|| 1||
Let's assume **LinkedWithBase1** model content_type id is **1** and
**LinkedWithBase2** content_type id is **2**.
Connection model
|| id || ct_id || object_id ||
|| 1 || 1 || 1||
|| 2 || 2 || 1||
**1 row is connected with id 1 of LinkedWithBase1 model
2 row is connected with id 1 of LinkedWithBase2 model**
When i was run below code
{{{
connections = Connection.objects.prefetch_related("object__f").all()
for i in connections:
print(i.object.f.value)
}}}
**Actual Output**
Foreign
Foreign
**Expected Output**
Foreign
OtherForeign
In 1st iteration **i.object.f** evaluate **Base1 model instance** which is
right
In 2nd iteration **i.object.f** it should be take **Base2 model instance**
but django takes as **Base1 model instance**
because of wrong prefetching occurs
But when i removed **f** from prefetch_related it gives me a expected
result
Ex: connections = Connection.objects.prefetch_related("object").all()
i am very confident prefetching went wrong because i spent 5 hours to
debug this issue in Django core and i got it doing wrong prefetching when
it has a multiple content types as same name of key like **f** attribute
in above code, I have patch for this.
I will very greatful if you give me a chance to fix this issue
--
Ticket URL: <https://code.djangoproject.com/ticket/34198>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/01070184d7afd70e-f9297412-fbe7-483a-9113-1f25f4d6fe6b-000000%40eu-central-1.amazonses.com.