Opened 9 years ago

Closed 9 years ago

#25198 closed Uncategorized (needsinfo)

Django's model_to_dict should return ALL fields, including hidden

Reported by: Anton Danilchenko Owned by: nobody
Component: Core (Serialization) Version: dev
Severity: Normal Keywords: model serialization
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Hello.

I found that Django return only editable fields in call of model_to_dict. I wish to have an option include_hidden=True to get list of ALL fields including not editable ones.

I will make this fix and tell you about fix.

Change History (11)

in reply to:  description comment:1 by Anton Danilchenko, 9 years ago

comment:2 by Tim Graham, 9 years ago

This function isn't a documented public API, so if you are using it in your own code, be aware that it could change without notice. I'm skeptical of adding parameters that Django itself doesn't use to such a function.

in reply to:  2 comment:3 by Anton Danilchenko, 9 years ago

Replying to timgraham:

This function isn't a documented public API, so if you are using it in your own code, be aware that it could change without notice. I'm skeptical of adding parameters that Django itself doesn't use to such a function.

I agree that this function isn' documented in Django docs, but me and a lot of people use this function in their work. This non-breaking change can simplify life for a lot of developers.

comment:4 by Tim Graham, 9 years ago

If it's widely used, then we should consider making it a public API and documenting it. Perhaps you could give your rationale on the DevelopersMailingList? If the function is meant as a general "serialization" utility as you categorized the ticket, forms.models seems like it might not be the best place to expose such functionality. Does the Python serializer in core.serializers not work for your use case?

in reply to:  4 comment:5 by Anton Danilchenko, 9 years ago

Replying to timgraham:

If it's widely used, then we should consider making it a public API and documenting it. Perhaps you could give your rationale on the DevelopersMailingList? If the function is meant as a general "serialization" utility as you categorized the ticket, forms.models seems like it might not be the best place to expose such functionality. Does the Python serializer in core.serializers not work for your use case?

Django serializer work very slow compared to my own implementation of serialization. I compared it with what I created manually and I can save about 50% of time on make response with serialized queryset. It's why I get serialized list of objects and convert them to list of values that I need.

Now I use this way to get list of model fields:

  def get_model_fields(obj):
      return sorted(options.concrete_fields, options.virtual_fields, options.many_to_many)

But it will be better to get list of ALL model fields as simple call model_to_dict(obj, include_hidden=True)

comment:6 by Tim Graham, 9 years ago

Would there be some value in trying to improve the speed of Django's serialization instead of needing to roll your own?

in reply to:  6 comment:7 by Anton Danilchenko, 9 years ago

Replying to timgraham:

Would there be some value in trying to improve the speed of Django's serialization instead of needing to roll your own?

In this task I don't propose to improve serializer. My change is simple and used widely. You can google "how to convert django model to dict" and se on stackoverflow how people do this.

comment:8 by Tim Graham, 9 years ago

Are you willing to write to the mailing list to describe your problem and elaborate on why model_to_dict() is the best solution such that it should be a public API? This discussion needs to involve more than just me and you. Thanks!

in reply to:  8 comment:9 by Anton Danilchenko, 9 years ago

Replying to timgraham:

Are you willing to write to the mailing list to describe your problem and elaborate on why model_to_dict() is the best solution such that it should be a public API? This discussion needs to involve more than just me and you. Thanks!

OK, will do. Please tell me the adress of mailing list. Do you mean Google Groups Django group?

comment:11 by Tim Graham, 9 years ago

Resolution: needsinfo
Status: newclosed

Closing, pending a discussion on the mailing list.

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