question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Best practice for nested data in POST

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
0xecutecommented, Feb 12, 2020

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.

0reactions
frolcommented, Jan 25, 2020

I am sorry, I won’t be able to help you with this.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found