#26296: Trick with extra fields
-------------------------------------+-------------------------------------
Reporter: vvkuznetsov | Owner:
Type: | vvkuznetsov
Cleanup/optimization | Status: assigned
Component: Documentation | Version: master
Severity: Normal | Resolution:
Keywords: db | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by vvkuznetsov):
* status: new => assigned
* needs_better_patch: => 0
* needs_tests: => 0
* owner: nobody => vvkuznetsov
* needs_docs: => 0
Old description:
> In documentation there's comment in `values()` section about some
> behavior of `extra`
>
> https://docs.djangoproject.com/en/1.9/ref/models/querysets/#django.db.models.query.QuerySet.values
>
> {{{
> If you use a values() clause after an extra() call, any fields defined by
> a select argument in the extra() must be explicitly included in the
> values() call. Any extra() call made after a values() call will have its
> extra selected fields ignored.
> }}}
>
> It's points that any extra fields after values() call will be ignored.
> But I think that it's not correct and need more description.
>
> For example it could be not obvious that next simple construction will
> cause exception and such an not obvious one!
>
> {{{
> User.objects.values('username').extra(select={'test_field':
> 1}).values('test_field', 'username')
>
> FieldError: Cannot resolve keyword 'test_field' into field. Choices are:
> date_joined, email, first_name, groups, id, is_active, is_staff,
> is_superuser, last_login, last_name, logentry, password, test_field,
> user_permissions, username
> }}}
>
> I want to point out that `test_field` is listed in choices that's very
> confusing. It happens because of all checks is runs against
> `extra_select` but error is uses `extra` as part of available choices.
>
> {{{
> sorted(list(get_field_names_from_opts(opts)) + list(self.extra)
> + list(self.annotation_select))
> }}}
>
> I can help you with this small change.
New description:
In documentation there's comment in `values()` section about some behavior
of `extra`
https://docs.djangoproject.com/en/1.9/ref/models/querysets/#django.db.models.query.QuerySet.values
{{{
If you use a values() clause after an extra() call, any fields defined by
a select argument in the extra() must be explicitly included in the
values() call. Any extra() call made after a values() call will have its
extra selected fields ignored.
}}}
It's points that any extra fields after values() call will be ignored. But
I think that it's not correct and need more description.
For example it could be not obvious that next simple construction will
cause exception and such an not obvious one!
{{{
User.objects.values('username').extra(select={'test_field':
1}).values('test_field', 'username')
FieldError: Cannot resolve keyword 'test_field' into field. Choices are:
date_joined, email, first_name, groups, id, is_active, is_staff,
is_superuser, last_login, last_name, logentry, password, test_field,
user_permissions, username
}}}
I want to point out that `test_field` is listed in choices that's very
confusing. It happens because of all checks is runs against `extra_select`
but error is uses `extra` as part of available choices.
{{{
sorted(list(get_field_names_from_opts(opts)) + list(self.extra)
+ list(self.annotation_select))
}}}
I can help you with this small change.
https://github.com/django/django/pull/6225
--
--
Ticket URL: <https://code.djangoproject.com/ticket/26296#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/069.fe348c1c2ea68c54adc8e15b30d4244e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.