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
1 answer

How to model a Pydantic Model to accept IP as either dict or as cidr string

In Pydantic, is it possible to pass a value that is not a dict and still make it go through a BaseModel? I have a case where I want to be able to process a CIDR formatted IP (e.g. 1.2.3.4/32) and still return a valid model Ipv4. In the example below…
RaamEE
  • 3,017
  • 4
  • 33
  • 53
0
votes
0 answers

JSON Schema - nested property is required based on conditional

I tried to create a minimum viable example for this. What I want to achieve is the following: if json contains post, then make both a and b required in attrs if json contains patch then make neither of them required. Use datamodel-codegen to…
frei
  • 495
  • 3
  • 19
0
votes
1 answer

Validation against current user in Pydantic model using FastAPI

I have a question and I'm much surprised that nobody asked it before. The question is: how to make validation against logged in user in Pydantic BaseModel? This is very easy in Django, where we can pass the request object to serializer, but I have…
Artem Chege
  • 189
  • 2
  • 5
0
votes
1 answer

Pydantic Inherited Class validation

I have 2 Pydantic classes class AuthenticatedCreateBasketV2ApiModel(PublicCreateBasketV2ApiModel): agency: str = Field() owner: Optional[UserUuid] = Field() effective_date: Date class PublicCreateBasketV2ApiModel(BaseModel): …
HITman
  • 43
  • 5
0
votes
0 answers

pydantic (different) Field name and/or alias in Model Config json_encoders

to talk to an foreign API I don't want/need the Submodel but only it's id. This is working well with using json_encoders in the Model Config. Because I only return the id I want a different alias (and maybe also name) for it. Any thoughts how I can…
Simon
  • 354
  • 3
  • 10
0
votes
0 answers

Pydantic ValidationError for inheritance(nested) model

There is a structure to models with inheritance class CampaignAdditionalData(BaseModel): campaigns: List[CampaignData] stages: List[CreativeUsageStage] language_mode: ReplicationLanguageMode creative_rotation_type:…
Jekson
  • 2,892
  • 8
  • 44
  • 79
0
votes
1 answer

in pydantic.validators.find_validators TypeError: issubclass() arg 1 must be a class

Hello I am reading a JSON with the following format: { "1": {"id":1, "type": "a"}, 2: {"id":2, "type": "b"}, "3": {"id":3, "type": "c"}, "5": {"id":4, "type": "d"} } As you can see the keys are numbers but are not consecutives. So I…
Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84
0
votes
1 answer

How to define a class with mutually exclusive members/fields in Python3/Pydantic

I have a class called GeoLocation with fields (point and region) that are mutually exclusive. How do I define the class? Python3 code: from enum import Enum from typing import Optional from pydantic import BaseModel class Point(BaseModel): …
0
votes
0 answers

How can I implement field level decorators in Python?

One of the things I like about Java/Spring is the support for field level decorators for MongoDB. Can something similar to what's below be achieved in Python (preferably using Pydantic)? class Customer(BaseModel): id: PyObjectId =…
Joe P
  • 499
  • 1
  • 4
  • 11
0
votes
1 answer

How to access fastapi.Request.state when using Pydantic.BaseModel

I'm trying to create a pydantic BaseModel that will be able to map some data from the request body and also from the request.state. How can this be accomplished?
0
votes
1 answer

ImportError: cannot import name 'Annotated' and 'ValueRange'

Hello I am using Python 3.8 And I am implementing a dataclass with a fix list in order to do this I have the following code: from dataclasses import dataclass from typing import Annotated, List, ValueRange from pydantic import…
Tlaloc-ES
  • 4,825
  • 7
  • 38
  • 84
0
votes
1 answer

constructing Pydantic schema for response modal fastapi

i have a response of data like this { "k9KNg_id": { "card_name": "item1", "price_updated_date": "2022-04-07T19:30:25.78Z", "card_img_id": "https://testingimg", "set_name": "some testing set", "set_tag": "mm3", "rarity":…
user1897151
  • 493
  • 2
  • 9
  • 28
0
votes
1 answer

Pydantic reusable Validation for Dictionary's keys and values

How to validate input to get the following Dict passed! d = dict() d['en'] = 'English content' d['it'] = 'Italian content' d['es'] = 'Spanish content' print(d) # {'en': 'English content', 'it': 'Italian content', 'es': 'Spanish content'} In this…
0
votes
0 answers

unhashable type: 'list' FastAPI PUT request

I am attempting to update an entry in my DB which should update the parent table projects as well as the child table rounds but I am getting this error 500: sqlalchemy.exc.StatementError: (builtins.TypeError) unhashable type: 'list' [SQL: UPDATE…
0
votes
1 answer

How can I find Pydantic's Basemodel field value by a STRING?

For example I have a pydantic model, like this: class Person: name:str age: int city: str With pydantic we can get the value by using: P1.name -for example P1 already exists as an instance- My question is that I have a P1 and I have a…
jhsznrbt
  • 133
  • 1
  • 13