Opened 2 years ago

Closed 2 years ago

#21700 closed Bug (wontfix)

model_to_dict return id key as unicode

Reported by: filip.f.novak@… Owned by: nobody
Component: Forms Version: master
Severity: Normal Keywords: models, model_to_dict
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


After converting the model instance using model_to_dict is the key 'id' unicode type (all others are string). It causes a problem when accessing this parameter for further conversion into namedtuple.
Are you thinking about adding a function model_to_namedtuple? Implementation would (after removing the problem of 'id') elementary.

Output example:
{'code': u'B-US', 'pieces_in_package': 10, u'id': 1}


Change History (1)

comment:1 Changed 2 years ago by charettes

  • Component changed from Database layer (models, ORM) to Forms
  • Easy pickings unset
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed
  • Version changed from 1.6 to master

Since model_to_dict is a private API (It has not been documented) and Django doesn't internally requires a model -> namedtuple serializer nor is affected by the unicode keys issue I don't think we're going to fix that.

I'm not saying such a serializer wouldn't be helpful for third party cases but model_to_dict is really just an internal function (that you shouldn't rely on since it might go away) used by model forms.

I'd suggest you write your own implementation of model_to_namedtuple for your specific needs.

Note: See TracTickets for help on using tickets.
Back to Top