37 lines
1.1 KiB
Python

```python
from typing import Optional
from pydantic import BaseModel
from datetime import datetime
class CommentBase(BaseModel):
content: str
class CommentCreate(CommentBase):
pass
class CommentUpdate(CommentBase):
pass
class Comment(CommentBase):
id: int
post_id: int
user_id: int
created_at: datetime
updated_at: Optional[datetime] = None
class Config:
orm_mode = True
```
Explanation:
1. We import the necessary modules: `typing` for type hints, `pydantic` for defining data models, and `datetime` for working with date and time objects.
3. `CommentCreate` inherits from `CommentBase` and is used for creating new comments. It doesn't add any additional fields.
4. `CommentUpdate` also inherits from `CommentBase` and is used for updating existing comments. It doesn't add any additional fields.
5. `Comment` inherits from `CommentBase` and represents a complete comment object. It includes fields like `id`, `post_id`, `user_id`, `created_at`, and `updated_at`.
6. The `Config` class inside `Comment` sets `orm_mode = True`, which allows Pydantic to work with SQLAlchemy models and handle datetime objects correctly.