3 ms·
Do you have a public repo with examples of this in action?
by pphysch 5y ago
Do you have a public repo with examples of this in action?
- IgorPartola 5y agoUnfortunately no, but I will see if I can at some point open it up. It’s used at $DAYJOB though the original inspiration came from a personal project so I need to untangle that. Frankly it’s not hard to do: you need a parser for author.books.chapters, then a bit of code to look up fields from a model’s Meta class, and then a recursive function to traverse model instances and include the correct field/value pairs based on the type that each field is (scalar, iterable, dict, model instance). My latest version includes a couple of niceties like the annotation on the models of fields to always include/exclude, support for custom properties, and caching of the above. The output is a dict or a list of dicts that can be serialized by Django’s JSON serializer. Django actually sort of includes a version of this already for serializing model instances, it’s just not very robust.