[Question] Best practice for nested data in POST
See original GitHub issueHi,
Imagine I have one team with a list of users.
So when I get the data, it will be something like this:
{id: 1, name:'team', users:[{id:1, username='john'}]}
the schema would be simple:
class BaseUserSchema(ModelSchema):
class Meta:
model = User
fields = (
User.id.key,
User.username.key
)
dump_only = (
User.id.key,
)
class BaseTeamSchema(ModelSchema):
users = base_fields.List(base_fields.Nested(BaseUserSchema))
class Meta:
model = Team
fields = (
Team.id.key,
Team.name.key,
Team.users.key,
)
dump_only = (
Team.id.key,
)
Now my question is, when I want to add a team, with some users, I want to send something like this:
{name:'team B', users:[{id:1}, {id:2}, {id:3}]}
with already existing users.
But, it will not have the users id, since it is in dump_only fields.
Even through I override it in CreateTeamParameters with
users = base_fields.List(base_fields.Nested(BaseUserSchema(only=['id'])))
it doesn’t override the dump_only.
What would be the best practice in order to Post nested schemas?
Thank you!
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
REST API Design Best Practices for Sub and Nested Resources
REST API Design Best Practices for Sub and Nested Resources · Client-Server Architecture · Statelessness · Cacheability · Layered System · Uniform ...
Read more >api design - What are best practices for REST nested resources?
I've tried both design strategies - nested and non-nested endpoints. I've found that: if the nested resource has a primary key and you...
Read more >REST API: POST and PUT for nested resources
What REST/HTTP don't tell you is how to design your resources. Should there be one resource, or many? Should information about cars be...
Read more >Best Practices for Designing Developer-Friendly REST APIs
A pragmatic guide into building great APIs by strictly following REST principles and optimising for developer experience.
Read more >Best practices for REST API design - Stack Overflow Blog
Learn how to design REST APIs to be easy to understand for anyone, future-proof, secure, and fast since they serve data to clients...
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
Okay, we were almost right. Using dump_only=[] should work, but the following code in marshmallow:
self.dump_only = set(dump_only) or set(self.opts.dump_only)
kind of fail. Since set([]) is equal to 0, it will go with the or. So you should just not use an empty array and do something like this for example:users = base_fields.List(base_fields.Nested(BaseUserSchema(only=['id'], dump_only=[None])))
Not the perfect solution, but it works and keep your basic schema secured by not accepting ID rewrite.Thank you again for the project.
I am sorry, I won’t be able to help you with this.