Questions tagged [pydantic]

Pydantic is a library for data validation and settings management based on Python type hinting and variable annotations. You can use Pydantic for defining schemas of complex structures in Python.

Pydantic is a library for data validation and settings management based on Python type hinting (PEP484) and variable annotations (PEP526). It allows for defining schemas in Python for complex structures.

1612 questions
0
votes
0 answers

Best practice to set default variable for pydantic field at runtime

I have a pydantic model for request like below, from pydantic import BaseModel, Field from typing import List, ClassVar from fastapi import FastAPI app = FastAPI() class myRequestModel(BaseModel): items: List[str] = Field(...,…
cointreau
  • 864
  • 1
  • 10
  • 21
0
votes
0 answers

Sharing .env file between apps in development

I'm trying to dockerize my project, and I've come up with the following file structure: project/ ├── app1/ │ ├── main.py │ ├── Dockerfile │ └── venv/ ├── app2/ │ ├── main.py │ ├── Dockerfile │ └── venv/ ├── .env └──…
Shmookoff
  • 146
  • 1
  • 10
0
votes
1 answer

pydantic nested model response

I want to pass nested models into the response. I am trying by making object of 1 model then storing the related values to it, making object of 2nd model and storing related values to it and at the last trying to store thoes 2 model in the response…
Sohaib Anwaar
  • 1,517
  • 1
  • 12
  • 29
0
votes
0 answers

Shared attribute in pydantic BaseModel

I have the following pydantic models class Bar(BaseModel): foo: int class Foo(BaseModel): bar: int ba: Bar Is there a way to access the bar attribute from the Foo model when running a validation on the foo attribute of the Bar…
Occhima
  • 153
  • 2
  • 9
0
votes
2 answers

How to check if my type t is surrounded by typing.List or not? For example check the difference between int and List[int]

I want to execute different code depending on the type of my t. I've got the follwing types: from pydantic import BaseModel from typing import List class Person(BaseModel): name: str id: int and t1 = Person t2 = List[Person] now I want to…
danielmoessner
  • 339
  • 3
  • 18
0
votes
1 answer

Pydantic mangling input

In the following example, I can't understand why pydantic is mangling the type input. from pydantic import BaseModel from typing import Union, List class Foo(BaseModel): bar: Union[str, dict, List[dict]] f = Foo(bar=[{'foo': 'bar', 'stuff':…
robcxyz
  • 74
  • 2
  • 11
0
votes
1 answer

Problem: responseModel being ignored. FastApi (python 3.8)

I am using FastApi and My responseModel is being ignored. I am attempting to NOT return the password field in the response. Why does FastApi ignore my responsemodel definition? Here is my api post method: @app.post("/users") def create_users(user:…
Mark Wardell
  • 493
  • 7
  • 25
0
votes
0 answers

Pydantic JSON validation

Currently i am working on a sample project using pydantic and starlette. This is code from main file: async def submit(request): response = await request.json() resume = Resume(**response) JsonResponse({"Hello":"World"}) Code from Validation…
0
votes
1 answer

InValid Pydantic Field Type POST parameters (FastApi) - Python 3.8

I am attempting to use FastApi + SqlAlchemy And am having an issue with Parameters. This is the Error my Post parameter is causing. What am i doing wrong? ERROR MESSAGE fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check…
Mark Wardell
  • 493
  • 7
  • 25
0
votes
1 answer

How to create a Python ABC interface pattern using Pydantic

I'm implementing a Python Interface using the abstract base class (known as the strategy pattern). I want to be able to do this with Pydantic. Without Pydantic, I would use properties, like this: from abc import ABC, …
John Kealy
  • 1,503
  • 1
  • 13
  • 32
0
votes
0 answers

Docker-Compose not creating schema from local db while building docker image

I have created a docker-compose file with 2 services web and POSTGRES. When I run the below docker-compose file, it does not create tables that is in my local POSTGRES db. I have provided the environment variables to use. I think it should use these…
Ziv
  • 95
  • 7
0
votes
1 answer

Write one of model's attributes to the database and then read entire model from this single attribute

I am trying to create a Currency model that wraps pycountry's Currency object. This model will include the currency code and full name. However, I only want to store the code in my database when I call the model's .dict() method. When this code is…
KOB
  • 4,084
  • 9
  • 44
  • 88
0
votes
4 answers

FastApi: 422 Unprocessable Entity

I'm getting this error while trying to accept a pedantic model. After debugging for quite some time I believe the problem is with accepting CodeCreate Pydantic model class BaseCode(BaseModel): index: Optional[int] = Field(None) email:…
ASAD HAMEED
  • 2,296
  • 1
  • 20
  • 36
0
votes
1 answer

How to partially update data with FastAPI and PyDantic BaseModel using PATCH

I'd like to partly update database via PATCH method in FastAPI. I use Postgres as my database, Postman to test. I followed the example on FastAPI document, link: https://fastapi.tiangolo.com/tutorial/body-updates/#partial-updates-with-patch I use…
Liu Yu
  • 391
  • 1
  • 6
  • 16
0
votes
1 answer

Flexible Schema - BaseModel of Pydantic

I'm using pydantic 1.9.0 and fastapi 0.71.0 with Python 3.7.8. Here's how I've defined my model: class PartModel(BaseModel): _id: str _key: str number: str = Field(...) name: str = Field(...) price: int = Field(...) class…
msp
  • 1