Exclude any FK fields in pydantic_model_creator
See original GitHub issueclass User(models.Model):
id = fields.IntField(pk=True)
username = fields.CharField(max_length=128, index=True, unique=True)
email = fields.CharField(max_length=128, unique=True, index=True)
class Event(models.Model):
id = fields.IntField(pk=True)
name = fields.CharField(max_length=128)
user = fields.ForeignKeyField("models.User", related_name="events")
It would be nice to add such a solution:
Event_Pydantic = pydantic_model_creator(Event, exclude=("user__email",))
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5
Top Results From Across the Web
pydantic exclude multiple fields from model - python
A possible solution is creating a new class based in the baseclass using create_model: from pydantic ...
Read more >Source code for tortoise.contrib.pydantic.creator
() #: Fields listed in this property will be excluded from pydantic model exclude: ... :param cls: The Tortoise Model :param name: Specify...
Read more >Pydantic serialisation — Tortoise ORM v0.17.3 Documentation
Tortoise ORM has a Pydantic plugin that will generate Pydantic Models from Tortoise Models, and then provides helper functions to serialise that model...
Read more >Django and Pydantic
With schemas, you can also define which fields should and shouldn't be included from a particular model by passing exclude or include to...
Read more >Body - Nested Models - FastAPI
With FastAPI, you can define, validate, document, and use arbitrarily deeply nested models (thanks to Pydantic). List fields¶. You can define an attribute...
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 Free
Top 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
You can already do that, but you need to use ‘.’ as separators:
Let’s say we have more model fields
Suppose we have a method in which I want to return a list of events and in this list I do not need additional information about the user, except for the id and username. If I use your way of processing, this will apply to all subsequent methods. In the
/ events /
method I will get the desired data, and in the/ profile /
method I need detailed information about the user.Such a solution will give more flexible pydantic models: