Is there a way to get a name of the field of the pydantic object without hardcoding it as a string? #8399
Unanswered
peterlitvak
asked this question in
Question
Replies: 2 comments 1 reply
-
this would be very useful - did you find a solution or workaround? |
Beta Was this translation helpful? Give feedback.
1 reply
-
I had a need for this too... This worked for me: from typing import Optional
from pydantic import BaseModel, Field
class SomeObj(BaseModel):
somevar: Optional[str] = Field(default=None)
obj = SomeObj()
print(obj.__class__.__name__) #__name__ is already a string before printing Output:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Often one needs to provide the name of the field to some downstream code, e.g.:
The class name can be obtained from the class metadata, but the field name has to be hard-coded.
It would be nice to have some abstraction around this, too.
Something like a synthetic field
User.username_info.name
or a similar ideaBeta Was this translation helpful? Give feedback.
All reactions