Opened 6 years ago
Closed 6 years ago
#30302 closed Cleanup/optimization (fixed)
model_to_dict() should return an empty dict for an empty list of fields.
Reported by: | Belegnar | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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:
Change History (7)
comment:1 by , 6 years ago
Type: | Bug → Cleanup/optimization |
---|---|
Version: | 2.1 → master |
comment:2 by , 6 years ago
Needs tests: | set |
---|---|
Summary: | forms.models.model_to_dict fails when `fields` is empty list → model_to_dict() should return an empty dict for an empty list of fields. |
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 6 years ago
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
comment:5 by , 6 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
model_to_dict()
is a part of private API. Do you have any real use case for passing empty list to this method?