{{{
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.
* 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>
* 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>