0

I have a use case where I am accepting data of different datatypes - namely dict, boolean, string, int, list - from the front end application to the FastAPI backedn using a pydantic model.

My question is how should I design my pydantic model so that it can accept any data type, which can later be used for manipulating the data and creating an API?

from pydantic import BaseModel

class Pino(BaseModel):
    asset:str (The data is coming from the front end ((dict,boolean,string,int,list))  )

@app.post("/api/setAsset")
async def pino_kafka(item: Pino):
    messages = {
        "asset": item.asset
}
Chris
  • 18,724
  • 6
  • 46
  • 80
Harry DSOUZA
  • 27
  • 1
  • 2
  • Does this answer your question? [Using different pydantic models depending on the value of the fields](https://stackoverflow.com/questions/71539448/using-different-pydantic-models-depending-on-the-value-of-the-fields) – Chris May 18 '22 at 10:32
  • No Chris , it dint.I would atleast need 4 types of data to accept this , 1) string 2)list of dictionary 3) dictionary 4) list . If I give Union[dict,list,string,int,bool] only the first two datatypes give the correct output , the string part will not work under this circumstances – Harry DSOUZA May 20 '22 at 18:04

1 Answers1

2

Define a custom datatype:

from typing import Optional, Union
my_datatype = Union[dict,boolean,string,int,list]

In python 3.9 onwards, you don't need Union any-more:

my_datatype = dict | boolean | string | int | list

Then use it in your model:

class Pino(BaseModel):
    asset: my_datatype

If you really want "any" datatype, just use "Any":

from typing import Any
class Pino(BaseModel):
    asset: Any

In any case, I hardly find a use case for this, the whole point of using pydantic is imposing datatypes.

Ziur Olpa
  • 1,839
  • 1
  • 12
  • 27
  • Thanks for your revert , However union will be able to take first two types rite ? which means if I give Union[dict,boolean,string,int,list] , this will only take in either dict or bool – Harry DSOUZA May 18 '22 at 08:02
  • I think it works also for multiple data types. – Ziur Olpa May 18 '22 at 08:05
  • it breaks in the middle : if we give input as list of dictionary for Union[dict,boolean,string,int,list] , only the dictionary part would be printed – Harry DSOUZA May 18 '22 at 12:37
  • Use dict[Any,Any] and list[Any], because dict and list are containers of other datatypes. if you really want a list of dictionaries put list[dict[str,str]] or list[dict[Any,Any]], depends on your needs. – Ziur Olpa May 18 '22 at 12:38
  • I would atleast need 4 types of data to work , 1) string 2)list of dictionary 3) dictionary 4) list . If I give Union[dict,list,string,int,bool] only the first two datatypes give the correct output , the string part will not work under this circumstances – Harry DSOUZA May 18 '22 at 12:43
  • When you say list of dictionary and dictionary, a dictionary is also container of datatypes, like dictionary of string-string. Anyway, can you put the trace back or the error you get? – Ziur Olpa May 18 '22 at 12:48
  • so ideal situation is union takes in only two data types ,If I consider the above example Union[dict,boolean,string,int,list] , the solution will work only for boolean and dict . To explain in depth , if the incoming data is {'key' : 'value'} or boolean value(true or false) I am able to send a post request , if the input data is List : for ex ['keyword1','keyword2'] or string "name" , I cannot post any data because the union what I defined is only accepting first two fields i.e either dict or boolean , If I interchange the datatype in union then it works for list and string but not (dict,bool) – Harry DSOUZA May 20 '22 at 18:16
  • Any solution you would propose – Harry DSOUZA May 22 '22 at 15:25