Hi,
Your data (JSON) structure is not ideal for automated handling,
specially you should try to conform to key-value pairs where key is
pre-known so your data should look something like:
[ {"person" : "kane", "data" : {"gender": "male", ...}, {"person" :
"tom", data: {...}}, ... ]
(or you could put name within other data and just have:
[{"name":"kane", "gender":...}, ["name":"tom", "gender":...}]
Having key as a value is a bit problematic, it's not impossible to do
but as Tom said, you'll end up writing custom
(de)serialiser.
(sequence, object,
> >> - have a default JSON serializer which returns everything in
> >> the JSON (regardless of it's structure), provided it's valid.
> >> - optionally add specify validators for certain fields, which
> >> may include "key X exists in the JSON, and it is a
> >> list/dictionary" (which I also can't seem to figure out how to do,
> >> without making a serializer for the property of key X).
> >>
> >> *Motivation*: we use dictionaries such as {"a": "blah1", "b":
> >> "blah2"} instead of lists of dictionaries such as [{"name": "a",
> >> "value": "blah1"},{"name": "b", "value": "blah2"}]. The simple
> >> reason that internally it's a lot easier for us to lookup values:
> >> in the first case I find the value for e.g. key "a", whereas in
> >> the second I have to loop through the list until I find the
> >> dictionary with name == "a", and then get it's "value" field. It's
> >> not too bad in this example, but with many keys/items, nested at
> >> multiple levels, using lists becomes unreasonable. (And no, none
> >> of these come from Django models.) That said, it doesn't seem to
> >> be compatible with django REST framework, mainly because I don't
> >> know the keys beforehand so I can't declare them as fields (though
> >> I do know what the structure that the values take). It also
> >> appears I can't use list serializers (i.e. many=True), as
> >> dictionaries aren't lists.
> >>
> >> *Possible approaches: *From what I've read/tried, I see three
> >> avenues:
> >>
> >>
> >> 1. Dynamically modify fields
> >> <
http://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields>:
> >> I've spent a long time with this by reading the dictionary to
> >> get all the keys and adding them all with the fields=() option. I
> >> thought I had it working, but it turned out it wasn't (it return
> >> null for all keys as soon as one key value wasn't serialized). I'd
> >> prefer to avoid this approach as it seems somewhat complex.
> >> 2. This
> >> <
https://groups.google.com/forum/#!searchin/django-rest-framework/serialize$20dictionary/django-rest-framework/f0Qftb1EdWw/vJpiT6zkIvYJ>
> >> question about changing the list method. I prefer this method, but
> >> I wasn't able to get an example working.
> >> 3. Construct something myself from the ground up. I may have
> >> to, but I imagine someone must have come across this same issue
> >> before ...
> >>
> >> Of course, I may have missed something obvious -- in which case my
> >> pride may be a little hurt, but I'll be darn glad to have an
> >> answer!
> >>
> >> So, could someone please enlighten me on the best way to solve this
> >> issue, or give me a nudge in the right direction?
> >>
> >> Thanks,
> >>
> >> Kane
> >>
> > --
> > You received this message because you are subscribed to a topic in
> > the Google Groups "Django REST framework" group.
> > To unsubscribe from this topic, visit
> >
https://groups.google.com/d/topic/django-rest-framework/zCyj4S8VJMc/unsubscribe
> > .
> > To unsubscribe from this group and all its topics, send an email to
> >
django-rest-fram...@googlegroups.com.
> > For more options, visit
https://groups.google.com/d/optout.
> >
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django REST framework" group. To unsubscribe from this group
> and stop receiving emails from it, send an email to