[Django] #27065: Deferred fields not passed to inherited models' __class__.__dict__

8 views
Skip to first unread message

Django

unread,
Aug 15, 2016, 1:09:30 AM8/15/16
to django-...@googlegroups.com
#27065: Deferred fields not passed to inherited models' __class__.__dict__
----------------------------------------------+----------------------------
Reporter: jarekwg | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.10
Severity: Normal | Keywords: defer
| inherited
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+----------------------------
Possibly a regression, possibly me misunderstanding how this should now
work in 1.10.

{{{
class A(models.Model):
name = models.CharField(max_length=20)
addy = models.CharField(max_length=200)


class B(A):
name2 = models.CharField(max_length=20)
}}}


Calling:
{{{
b = B.objects.only('name').first()
}}}
we expect `name2` and `addy` to get deferred. Calling
`b.get_deferred_fields()`, we get both these field names returned as
expected.

However, inspecting `b.__class__.__dict__`, we see that only the
`DeferredAttribute` for `name2` is available. To get the one for `addy`,
we need to consult `b`'s parent. This was not the case in 1.9.

See my [https://github.com/carljm/django-model-utils/pull/233 PR] to
django-model-utils for context.

--
Ticket URL: <https://code.djangoproject.com/ticket/27065>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 15, 2016, 7:55:39 AM8/15/16
to django-...@googlegroups.com
#27065: Deferred fields not passed to inherited models' __class__.__dict__
-------------------------------------+-------------------------------------

Reporter: jarekwg | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:

Keywords: defer inherited | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

I'd guess 7f51876f99851fdc3fef63aecdfbcffa199c26b9 is the commit in
question. Can you come up with a test case that demonstrates a bug in
normal Django usage? If not, that change sounds like an implementation
detail. We might mention it in the release notes if you feel it might
affect other projects.

--
Ticket URL: <https://code.djangoproject.com/ticket/27065#comment:1>

Django

unread,
Aug 16, 2016, 10:04:45 PM8/16/16
to django-...@googlegroups.com
#27065: Deferred fields not passed to inherited models' __class__.__dict__
-------------------------------------+-------------------------------------
Reporter: jarekwg | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: defer only | Triage Stage:
inherited DeferredAttribute | Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jarekwg):

* keywords: defer inherited => defer only inherited DeferredAttribute
* status: new => closed
* resolution: => invalid


Comment:

Thanks, I've verified it's definitely commit
7f51876f99851fdc3fef63aecdfbcffa199c26b9 that causes this change in
behaviour.

`DeferredAttribute` instances inherited from parent models can no longer
be obtained through `instance.__class__.__dict__` because the class is
[https://github.com/django/django/commit/7f51876f99851fdc3fef63aecdfbcffa199c26b9
#diff-1e7fc0d7d1b36358e371fab97bd1ddb1L236 no longer dynamically created],
but rather has the `DeferredAttribute` instances
[https://github.com/django/django/commit/7f51876f99851fdc3fef63aecdfbcffa199c26b9
#diff-bf776a3b8e5dbfac2432015825ef8afeR695 stapled on] with `setattr`.

However, thanks to
[https://github.com/django/django/commit/7f51876f99851fdc3fef63aecdfbcffa199c26b9
#diff-1e7fc0d7d1b36358e371fab97bd1ddb1R99 this change], a handle to the
`DeferredAttribute` can be obtained by accessing the field directly from
the instance's class, which looks considerably nicer and ''does'' pull
through inherited fields.

So
`deffered_field = instance.__class__.__dict__.get(field_name) # pre
dj110`
becomes
`deferred_field = getattr(instance.__class__, field_name) # dj110+`

I guess it can't hurt mentioning this in the release notes, though at the
same time I don't see particularly many projects that would need to handle
the `DeferredAttributes` directly. You're in a better position to make
that call. At the very least this ticket can serve as an answer to people
who google this problem.

Closing.

--
Ticket URL: <https://code.djangoproject.com/ticket/27065#comment:2>

Reply all
Reply to author
Forward
0 new messages