Code

Opened 5 months ago

Closed 3 weeks 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 5 months ago.
Defer test models.py
defer_tests.py (1.1 KB) - added by lovasb 5 months ago.
Defer tests.py
patch.py (579 bytes) - added by lovasb 5 months ago.
Patch.py

Download all attachments as: .zip

Change History (10)

Changed 5 months ago by lovasb

Defer test models.py

Changed 5 months ago by lovasb

Defer tests.py

Changed 5 months ago by lovasb

Patch.py

comment:1 follow-up: Changed 5 months ago by bmispelon

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 follow-up: Changed 5 months ago by bmispelon

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

comment:3 in reply to: ↑ 1 Changed 5 months ago by lovasb

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

comment:4 in reply to: ↑ 2 Changed 5 months ago by lovasb

Replying to bmispelon:

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

Yeah, that's mine! :)

comment:5 Changed 4 months ago by anonymous

  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 3 months ago by timo

  • Component changed from Uncategorized to Database layer (models, ORM)

comment:7 Changed 3 weeks ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 9385aa3198e7ab460990dd8f82e466fd243557a0:

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.