How to define a schema with dynamic field names
See original GitHub issueHi all,
I have the need to define a schema that basically represents a dict
object where I know that the keys
are going to be strings and the values
are all going to have the same object format.
I know that having this defined as a field of a schema can be done by using the fields.Dict
just like
class ValueSchema(Schema):
att_1 = fields.String()
att_2 = fields.String()
class RootSchema(Schema):
root = fields.Dict(keys=fields.String(), values=fields.Nested(ValueSchema))
# dict conforming to the schema
{
"root": {
"id-1":{"att_1":"a", "att_2":"b"},
"id-2":{"att_1":"c", "att_2":"d"}
}
}
But what I want is to have a schema that represents just the root
object, i.e. without the root
field, just like
{
"id-1": {"att_1":"a", "att_2":"b"},
"id-2": {"att_1":"c", "att_2":"d"},
(...)
"id-N": {"att_1":"x", "att_2":"y"},
}
Is this something that can be done with marshmallow?
Thanks in advance
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Dynamic Fields | Apache Solr Reference Guide 6.6
Dynamic fields allow Solr to index fields that you did not explicitly define in your schema. This is useful if you discover you...
Read more >Can Avro schema have unknown / dynamic field names?
Show activity on this post. id and name fields are always known, but custom_fields names are dynamic and can have any key name....
Read more >Dynamically adding fields in the schema doesn't permit to add ...
Hi, When I dynamically add a field to a schema and instantiate it to a model, mongoose is not ... Schema({ name: String,...
Read more >How do I get a dynamic list of object field names to present in ...
You want to have an Apex Controller to get the field names and return a type that a Lightning Aura Component (LAC) component...
Read more >How to use the dynamic field of SOLR - CodeLibs Project
dynamic fields and standard fields such as title and content can be freely defined field names are defined. The dynamic fields that are...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You can use
Schema.from_dict()
after you receive the data, only use the generated schema for that data, then generate a new schema when you get new data.The only remaining issue seems to be preferred syntax. I would make a new decorator for
from_dict
rather than trying to overload a schema class so that you can pass it into a decorator expecting a schema class. Feedback on how you ended up working around it would be appreciated.I am closing this for now. If you have a proposal that wasn’t already discussed in #972 I am happy to reopen. I still promote unifying the
load
/dump
schema interface with the field interface since it improves many other use cases, but the scope of that would probably justify a new issue.