jazzband / jsonmodels

jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
http://jsonmodels.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
334 stars 51 forks source link

Please, add DictField field #66

Closed Nekmo closed 7 years ago

Nekmo commented 8 years ago

Similar to ListField, but the keys are unique strings. This ensures that it is a primary key and easy access. For Example:

class Config(models.Base):
    users = DictField([User])

It will be serialized as:

{"users": {"Lucy": {"password": "...."}, "Bill": {"password": "......"}}}
beregond commented 8 years ago

@Nekmo you could use EmbeddedField for that with special model that keeps structure (and keys you are talking about).

Unless you want to have this dynamic, as in example?

beregond commented 7 years ago

I'm closing this issue, since I find this very non-JSON like (I tried "dict approach" few times - it is usually too hard to expand in future, plus you'd loose ability to validate it properly with json schema).

jonnyyu commented 6 years ago

I'm wondering how to define a model for a string dictionary like below. The keys are dynamic, and I need validation to check value is JSON serializable type.

{
   "environment": {
       "HOME": "/home/user",
       "MY_VAR": "1"
    }
}
dsully commented 6 years ago

+1 - I have some dynamic data. What's the right way to handle that?

FWIW, 'jsonobject' has a DictProperty which accomplishes this. Unfortunately that module doesn't work for my purposes due to other reasons.