I have a class model using Pydantics. I try to supply my own ID but it gives me two id fields in the MongoDB database. The one I gave it and the one it makes automatically.
Here is the result of my post method:
here is my class in models/articleModel.py:
class ArticleModel(BaseModel):
_id: int
title: str
body: str
tags: Optional[list] = None
datetime: Optional[datetime] = None
caption: Optional[str] = None
link: Optional[str] = None
class Config:
orm_mode = True
allow_population_by_field_name = True
arbitrary_types_allowed = True
here is my code for the post method in routers/article_router:
@router.post("/article/", status_code=status.HTTP_201_CREATED)
def add_article(article: articleModel.ArticleModel):
article.datetime = datetime.utcnow()
try:
result = Articles.insert_one(article.dict())
pipeline = [
{'$match': {'_id': result.inserted_id}}
]
new_article = articleListEntity(Articles.aggregate(pipeline))[0]
return new_article
except DuplicateKeyError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT,
detail=f"Article with title: '{article.id}' already exists")