Opened 10 years ago

Closed 10 years ago

#22050 closed Bug (fixed)

defer('related__subfield') and select_related() on proxy model raises error

Reported by: lovasb Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords: defer, proxy, query
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Given model (in the defer test directory):

class Secondary(models.Model):
    first = models.CharField(max_length=50)
    second = models.CharField(max_length=50)


@python_2_unicode_compatible
class Primary(models.Model):
    name = models.CharField(max_length=50)
    value = models.CharField(max_length=50)
    related = models.ForeignKey(Secondary)

    def __str__(self):
        return self.name
        
class PrimaryProxy(Primary):
    class Meta:
        proxy = True

Select only() on related models: PrimaryProxy.objects.all().select_related().only('related__first')
Raises KeyError:

E
======================================================================
ERROR: test_defer_proxy_related_field (defer.tests.DeferTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/lovasb/WORK/Personal/django/tests/defer/tests.py", line 176, in test_defer_proxy_related_field
    self.assertEqual(len(children), 1)
  File "/home/lovasb/WORK/Personal/django/django/db/models/query.py", line 122, in __len__
    self._fetch_all()
  File "/home/lovasb/WORK/Personal/django/django/db/models/query.py", line 963, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/lovasb/WORK/Personal/django/django/db/models/query.py", line 222, in iterator
    only_load = self.query.get_loaded_field_names()
  File "/home/lovasb/WORK/Personal/django/django/db/models/sql/query.py", line 1819, in get_loaded_field_names
    self.deferred_to_data(collection, self.get_loaded_field_names_cb)
  File "/home/lovasb/WORK/Personal/django/django/db/models/sql/query.py", line 602, in deferred_to_data
    must_include[old_model].add(source)
KeyError: <class 'defer.models.PrimaryProxy'>

Attachments (3)

defer_models.py (326 bytes ) - added by lovasb 10 years ago.
Defer test models.py
defer_tests.py (1.1 KB ) - added by lovasb 10 years ago.
Defer tests.py
patch.py (579 bytes ) - added by lovasb 10 years ago.
Patch.py

Download all attachments as: .zip

Change History (10)

by lovasb, 10 years ago

Attachment: defer_models.py added

Defer test models.py

by lovasb, 10 years ago

Attachment: defer_tests.py added

Defer tests.py

by lovasb, 10 years ago

Attachment: patch.py added

Patch.py

comment:1 by Baptiste Mispelon, 10 years ago

Hi,

Could you submit a pull request for this, or at least consolidate all the patches into one? It would make reviewing the ticket easier.

Thanks.

comment:2 by Baptiste Mispelon, 10 years ago

Looks like there's one already: https://github.com/django/django/pull/2279

in reply to:  1 comment:3 by lovasb, 10 years ago

Replying to bmispelon:

Hi,

Could you submit a pull request for this, or at least consolidate all the patches into one? It would make reviewing the ticket easier.

Thanks.

Hi,

Here is my pull request: https://github.com/django/django/pull/2279/files

Bence

in reply to:  2 comment:4 by lovasb, 10 years ago

Replying to bmispelon:

Looks like there's one already: https://github.com/django/django/pull/2279

Yeah, that's mine! :)

comment:5 by anonymous, 10 years ago

Triage Stage: UnreviewedAccepted

comment:6 by Tim Graham, 10 years ago

Component: UncategorizedDatabase layer (models, ORM)

comment:7 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In 9385aa3198e7ab460990dd8f82e466fd243557a0:

Fixed #22050 -- Fixed defer fields on proxy related models.

Note: See TracTickets for help on using tickets.
Back to Top