def get_annotations(main_cls):
ret_val = main_cls.__annotations__
for base_cls in main_cls.__bases__:
if base_cls != BaseModel:
ret_val.update(get_annotations(base_cls))
return ret_val
< /code>
und erstellte optionales Modell als < /p>
OptionalClientModel = create_model(
"OptionalClientModel",
**{k: (Optional[v], None) for k, v in get_annotations(ClientModel).items()})
< /code>
Die Originalklassen finden Sie wie unten < /p>
from typing import Annotated
from bson import ObjectId
from pydantic import Field
from pydantic import EmailStr
from pydantic import BaseModel
from pydantic import BeforeValidator
from pydantic import ConfigDict
from pydantic import AwareDatetime
from pydantic import field_validator
# Represents an ObjectId field in the database.
# It will be represented as a `str` on the model so that it can
# be serialized to JSON.
PyObjectId = Annotated[str, BeforeValidator(str)]
class DBTableBase(BaseModel):
# The primary key for the Table, stored as a `str` on the instance.
# This will be aliased to `_id` when sent to MongoDB,
# but provided as `id` in the API requests and responses.
id: PyObjectId | None = Field(alias="_id",
serialization_alias="id",
default=None)
model_config = ConfigDict(
json_encoders={ObjectId: str},
json_schema_extra={
"example": {
"id": "BSON_ID"
}
},
)
class ClientModel(DBTableBase):
first_name: str
last_name: str
Wenn ich ein Modell mit allen optionalen Wert möchte, kann ich OptionalClientModel .>
Ich folge https://stackoverflow.com/a/77851176/243031, um mein Modell optional zu erstellen.[code]def get_annotations(main_cls): ret_val = main_cls.__annotations__ for base_cls in main_cls.__bases__: if base_cls != BaseModel: ret_val.update(get_annotations(base_cls)) return ret_val < /code> und erstellte optionales Modell als < /p> OptionalClientModel = create_model( "OptionalClientModel", **{k: (Optional[v], None) for k, v in get_annotations(ClientModel).items()}) < /code> Die Originalklassen finden Sie wie unten < /p> from typing import Annotated from bson import ObjectId
from pydantic import Field from pydantic import EmailStr from pydantic import BaseModel from pydantic import BeforeValidator from pydantic import ConfigDict from pydantic import AwareDatetime from pydantic import field_validator
# Represents an ObjectId field in the database. # It will be represented as a `str` on the model so that it can # be serialized to JSON. PyObjectId = Annotated[str, BeforeValidator(str)]
class DBTableBase(BaseModel): # The primary key for the Table, stored as a `str` on the instance. # This will be aliased to `_id` when sent to MongoDB, # but provided as `id` in the API requests and responses. id: PyObjectId | None = Field(alias="_id", serialization_alias="id", default=None) model_config = ConfigDict( json_encoders={ObjectId: str}, json_schema_extra={ "example": { "id": "BSON_ID" } }, )
class ClientModel(DBTableBase): first_name: str last_name: str [/code] Wenn ich ein Modell mit allen optionalen Wert möchte, kann ich OptionalClientModel .>
Ich folge um mein Modell optional zu erstellen. def get_annotations(main_cls):
ret_val = main_cls.__annotations__
for base_cls in main_cls.__bases__:
if base_cls != BaseModel:...
Ich arbeite also im Grunde genommen an einem Laravel 11 -Projekt und das Sitzungs -Cookie wird von einem Portal generiert, aus dem ich auf meine Anwendung zugegriffen habe. 'SSO' , der für die...
Ich entwickle eine Anwendung in ASP.NET Core unter Verwendung von Identity zur Benutzerverwaltung und Authentifizierung. Ich versuche jedoch zuzulassen, dass das Feld „Benutzername“ nicht eindeutig...
Ich habe eine Laravel 5.3-App und ein Update-Formular, über das ich Benutzerprofilwerte sende. Eines davon ist auch das Benutzerbild. Ich erstelle ein verstecktes Eingabefeld dafür. Wenn der Benutzer...