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.

Exclude any FK fields in pydantic_model_creator

See original GitHub issue
class 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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
marcoaaguiarcommented, May 2, 2021

You can already do that, but you need to use ‘.’ as separators:

Event_List_Pydantic = pydantic_model_creator(Event, exclude=("user.email", "user.is_staff", "user.is_superuser",))
1reaction
AntonZNcommented, Feb 23, 2021

Let’s say we have more model fields

class User(models.Model):
    id
    username
    email
    is_staff
    is_superuser
    user_permissions
    
     class PydanticMeta:
        exclude = ("email", "is_staff", "is_superuser", "user_permissions" )

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:

User_Pydantic = pydantic_model_creator(User)
Event_List_Pydantic = pydantic_model_creator(Event, exclude=("user__email", "user__is_staff", "user__is_superuser",))
Event_Single_Pydantic = pydantic_model_creator(Event)
Read more comments on GitHub >

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

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