[Django] #30302: forms.models.model_to_dict fails when `fields` is empty list

57 views
Skip to first unread message

Django

unread,
Mar 31, 2019, 1:33:58 PM3/31/19
to django-...@googlegroups.com
#30302: forms.models.model_to_dict fails when `fields` is empty list
--------------------------------------+------------------------
Reporter: Belegnar | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------+------------------------
Been called as `model_to_dict(instance, fields=[])` function should return
empty dict, because no fields were requested. But it returns all fields
The problem point is

`if fields and f.name not in fields:`

which should be
`if fields is not None and f.name not in fields:`

PR: https://github.com/django/django/pull/11150/files

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

Django

unread,
Apr 1, 2019, 2:40:57 AM4/1/19
to django-...@googlegroups.com
#30302: forms.models.model_to_dict fails when `fields` is empty list
-------------------------------------+-------------------------------------
Reporter: Belegnar | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Forms | Version: master
Severity: Normal | Resolution:

Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* version: 2.1 => master
* type: Bug => Cleanup/optimization


Comment:

`model_to_dict()` is a part of private API. Do you have any real use case
for passing empty list to this method?

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

Django

unread,
Apr 1, 2019, 2:46:49 AM4/1/19
to django-...@googlegroups.com
#30302: model_to_dict() should return an empty dict for an empty list of fields.
--------------------------------------+------------------------------------
Reporter: Belegnar | Owner: nobody
Type: Cleanup/optimization | Status: new

Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

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

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

* stage: Unreviewed => Accepted
* needs_tests: 0 => 1


Comment:

[https://github.com/django/django/pull/11150 PR]

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

Django

unread,
Apr 1, 2019, 8:25:39 AM4/1/19
to django-...@googlegroups.com
#30302: model_to_dict() should return an empty dict for an empty list of fields.
--------------------------------------+------------------------------------
Reporter: Belegnar | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------

Comment (by Belegnar):

This method is comfortable to fetch instance fields values without
touching ForeignKey fields. List of fields to be fetched is an attr of the
class, which can be overridden in subclasses and is empty list by default

Also, patch been proposed is in chime with docstring and common logic

--
Ticket URL: <https://code.djangoproject.com/ticket/30302#comment:3>

Django

unread,
Apr 2, 2019, 5:48:17 PM4/2/19
to django-...@googlegroups.com
#30302: model_to_dict() should return an empty dict for an empty list of fields.
--------------------------------------+------------------------------------
Reporter: Belegnar | Owner: nobody

Type: Cleanup/optimization | Status: new
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Belegnar):

* needs_tests: 1 => 0


Comment:

[https://github.com/django/django/pull/11163 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/30302#comment:4>

Django

unread,
Apr 3, 2019, 3:12:15 AM4/3/19
to django-...@googlegroups.com
#30302: model_to_dict() should return an empty dict for an empty list of fields.
-------------------------------------+-------------------------------------
Reporter: Belegnar | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/30302#comment:5>

Django

unread,
Apr 3, 2019, 3:27:55 AM4/3/19
to django-...@googlegroups.com
#30302: model_to_dict() should return an empty dict for an empty list of fields.
-------------------------------------+-------------------------------------
Reporter: Belegnar | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: Forms | Version: master
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"1ffddfc233e2d5139cc6ec31a4ec6ef70b10f87f" 1ffddfc]:
{{{
#!CommitTicketReference repository=""
revision="1ffddfc233e2d5139cc6ec31a4ec6ef70b10f87f"
Fixed #30302 -- Fixed forms.model_to_dict() result if empty list of fields
is passed.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30302#comment:7>

Django

unread,
Apr 3, 2019, 3:27:55 AM4/3/19
to django-...@googlegroups.com
#30302: model_to_dict() should return an empty dict for an empty list of fields.
-------------------------------------+-------------------------------------
Reporter: Belegnar | Owner: nobody
Type: | Status: new

Cleanup/optimization |
Component: Forms | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"714cf468e10ccbfca6a97095939563a52b99e2eb" 714cf468]:
{{{
#!CommitTicketReference repository=""
revision="714cf468e10ccbfca6a97095939563a52b99e2eb"
Refs #30302 -- Added more tests for forms.model_to_dict().
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30302#comment:6>

Reply all
Reply to author
Forward
0 new messages