Invalid results with only() when model has two foreign keys to other models that inherit from a single abstract parent
Given the models:
from django.db import models
class Base(models.Model):
name = models.CharField(max_length=10)
lots_of_text = models.TextField()
class Meta:
abstract = True
def __unicode__(self):
return self.name
class A(Base):
a_field = models.CharField(max_length=10)
class B(Base):
b_field = models.CharField(max_length=10)
class C(Base):
a = models.ForeignKey(A)
b = models.ForeignKey(B)
is_published = models.BooleanField()
the following occurs:
# all ok here:
>>> from somewhere.models import A, B, C
>>> results = C.objects.all().select_related()
>>> results[0].a.id
2
>>> results[0].a.lots_of_text
u'Sed ut perspiciatis unde omnis iste natus error...'
# bizarre things happen: `id` and `lots_of_text` are swapped
>>> results = C.objects.all().only('name', 'a', 'b').select_related()
>>> results[0].a.lots_of_text
2
>>> results[0].a.id
u'Sed ut perspiciatis unde omnis iste natus error...'
# no luck in deferring two relations with only():
>>> C.objects.all().only('name', 'a', 'b').select_related().only('a__name', 'b__name')
Traceback (most recent call last):
...
FieldDoesNotExist: A has no field named 'b'
# deferring a single relation with only works:
>>> C.objects.all().only('name', 'a', 'b').select_related().only('a__name')
[<C_Deferred_b_is_published_lots_of_text_name: c1>, ...]
Change History
(7)
Summary: |
limit() behaves erratically in a queryset where the model has two foreign keys → only() behaves erratically in a queryset where the model has two foreign keys
|
Summary: |
only() behaves erratically in a queryset where the model has two foreign keys → invalid results with only() when model has two foreign keys to other models that inherit from a single abstract parent
|
Summary: |
invalid results with only() when model has two foreign keys to other models that inherit from a single abstract parent → Invalid results with only() when model has two foreign keys to other models that inherit from a single abstract parent
|
Resolution: |
→ fixed
|
Status: |
new → closed
|
Keywords: |
efficient-admin added
|
(In [10383]) Fixed deferred fields and select_related() interaction.
Loading related models when some fields were deferred was resulting in
incorrect offsets being used into the results row, causing the wrong data to be
assigned to attributes.
Refs #10710. This fixes the first of two bugs reported there.