SQLAlchemy model classes पेस्ट करें और column attribute नामों से keyed उदाहरण JSON पाएं। यह mapped_column() के साथ 2.0-स्टाइल Mapped[...], 1.x-स्टाइल Column(), और Flask-SQLAlchemy का db.Column() पढ़ता है, और mixins व parent classes के columns भी मिलाता है।
SQLAlchemy models सीधे json.dumps को नहीं दिए जा सकते, इसलिए FastAPI और Flask apps Pydantic या Marshmallow schemas जोड़ते हैं। वह schema लिखने से पहले यह तय करने के लिए कि model कौन से fields expose करेगा, या tests के लिए request bodies बनाने के लिए इसका इस्तेमाल करें।
SQLAlchemy बॉक्स में पेस्ट करें। Base, mixins और enum.Enum classes को उसी इनपुट में रखें, ताकि inherit हुए columns और enum values resolve हों।__tablename__ वाली classes ही models गिनी जाती हैं। कई models class नाम से निकलते हैं, जैसे { "User": {...}, "Team": {...} }।class TimestampMixin:
created_at = db.Column(db.DateTime, default=datetime.utcnow)
class Plan(enum.Enum):
FREE = "free"
PRO = "pro"
class Subscription(TimestampMixin, db.Model):
__tablename__ = "subscriptions"
id = db.Column(db.Integer, primary_key=True)
plan = db.Column(db.Enum(Plan), nullable=False)
seats = db.Column(db.SmallInteger, default=1)
trial_ends = db.Column(db.Date)
user_id = db.Column(db.Integer, db.ForeignKey("users.id"))
user = db.relationship("User", backref="subscriptions"){
"created_at": "1970-01-01T00:00:00",
"id": 0,
"plan": "free",
"seats": 1,
"trial_ends": "1970-01-01",
"user_id": 0
}Inheritance क्रम के अनुसार TimestampMixin का created_at पहले आता है, और user हट जाता है क्योंकि relationship() column नहीं है। db.Enum(Plan) enum की पहली वैल्यू "free" लेता है, और db.Date 1970-01-01 बनता है। seats default=1 से 1 दिखाता है, जबकि created_at placeholder रखता है क्योंकि datetime.utcnow एक function है।
Mapped[int], Mapped[Optional[str]] और Mapped[str | None] जैसे Python type hints वैल्यू तय करते हैं।Column(String(50)) या db.Column(db.Numeric) में।DateTime → 1970-01-01T00:00:00, Date → 1970-01-01, Time → 00:00:00, JSON/JSONB → {}, Uuid → पूरी तरह zero वाली UUID।ARRAY(String) और Mapped[list[str]] एक element वाले arrays बनते हैं।enum.Enum को extend करने वाली class, जो Enum(Plan) या Mapped[Plan] से इस्तेमाल हो, अपने पहले member की वैल्यू देती है।relationship(), column_property(), सादे class variables और __table_args__ जैसे dunder attributes बाहर रखे जाते हैं।FastAPI के response_model के लिए Pydantic class लिखने से पहले जांचें कि model JSON के रूप में कौन से fields और types expose करता है। Pydantic draft भी पाने के लिए वह JSON JSON to Python पेज में डालें।
client.post("/subscriptions", json=...) की body ऐसी keys से बनाएं जो model के attributes से मेल खाती हों। पहले autoincrement id और बनने का समय हटा दें।
default= ("new", 1, True, None) sample वैल्यू के रूप में इस्तेमाल होता है; datetime.utcnow या dict जैसे callables चलाए नहीं जाते। server_default= और nullable= नहीं पढ़े जाते।Column("user_name", String), तो JSON key फिर भी Python attribute नाम ही होती है।relationship से load होने वाले जुड़े objects शामिल नहीं होते। अगर आपका response उन्हें embed करता है तो nested objects खुद जोड़ें।hybrid_property या @property जैसे computed attributes आउटपुट का हिस्सा नहीं हैं।Mapped[Optional[str]] null की बजाय खाली string क्यों है?Optional का मतलब है कि None की अनुमति है; उदाहरण वह स्थिति दिखाता है जब वैल्यू मौजूद हो, इसलिए अंदर का str इस्तेमाल होता है। वह स्थिति चाहिए तो आउटपुट में उसे null कर दें।
Z या offset क्यों नहीं है?DateTime columns default रूप से naive होते हैं, और isoformat() उन्हें बिना offset के छापता है। अगर आप DateTime(timezone=True) इस्तेमाल करते हैं, तो +00:00 वाला उदाहरण असल में दिखने वाले के ज़्यादा करीब है।
नहीं। यह पेज सिर्फ़ SQLAlchemy का Column, mapped_column और Mapped syntax समझता है; models.CharField जैसे Django fields पहचाने नहीं जाते।