Creating additional fields on the fly
See original GitHub issueI’m trying to create a very flexible serializer, such that users can generate additional fields in the future. Let’s say that today they only need the defaults I’ve provided
class PostSerializer(Serializer):
id = fields.String()
title = fields.String(default="Untitled")
body = fields.String(default=None)
author = fields.List(fields.String)
The user creates several posts, and they decide they want a field for “category.” I provide an interface where they set a new category field. Now perhaps I store this field in a dictionary.
additional_fields = {
"category" : "list"
}
When I modify the serializer on the fly (the only way that seems to work is via Meta.additional, setattr
never seems to work)
s = PostSerializer
PostSerializer.Meta.additional = additional_fields.keys()
Posts which were created without the ‘category’ field will cause the following AttributeError:
AttributeError: "category" is not a valid field for {'id': '123456', 'title': 'Cool Post', 'body': 'Lorem Ipsum...', 'author': ['John', 'Steve']}
How can I maintain flexibility to add user generated fields, but also protect myself in the future? Is there a way to set a global default for additional fields?
Issue Analytics
- State:
- Created 9 years ago
- Comments:12 (5 by maintainers)
Top Results From Across the Web
Create additional input fields on the fly - Plumsail Community
Hi there! Is there anyway to create additional input fields on the fly with a button (unsure if there is a term for...
Read more >Allow Users to add Fields on the Fly | Jira Software Cloud
Hi,. I have different use cases when one of my teams need to add multiple values for the same field, but the number...
Read more >Add a field on the fly ‒ Qlik Sense for developers - Qlik | Help
1. Add the field Lengths in the app. ... The field is successfully added to the app. 2. Create a list object in...
Read more >How to add new fields to current Form on the fly?
In the browser right click on the newly generated filed and select "inspect element " from menu. The inspector panel will open up...
Read more >Create Multiple Fields on the Fly... | Adobe Acrobat
Ideally I am looking for "A FIELD" that I can create on PDF Form and subsequent fields can be created multiple times one...
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
@skqr You can use a @post_load method to do the same thing as
data_handler
.Yet another alternative is to update a schema’s fields on
__init__
.@MrYoda Here is an example of how to do that (if I understand problem correctly):
Alternatively, you can use special type to mark localized strings: