A
A
AzazeIo2021-07-01 19:03:23
Python
AzazeIo, 2021-07-01 19:03:23

How to upload an image using python+pydentic?

I have something like this model in which data is generated for news on the portal:

class  News:
    id: int
    name: str
    image = Path
    description: str
    start_date: str
    end_date: str
    published: int

    def generate_news_data(self) -> News:
        text = text_generator.suffix_generator()
        self.name: str = " Тестовая новость + "_" + suffix
        self.image: Path = Path("news_portal_project/project/main/news_logo.png")
        self.description: str = "Тестовое мероприятие"
        self.start_date: str = date_settings.generate_date()
        self.end_date: str = date_settings.generate_date(relative_delta=1)
        self.published: int = 1
        return self

    def get_news_data(self) -> dict:
        return {
            "name": self.name,
            "description": self.description,
            "startDate": self.start_date,
            "endDate": self.end_date,
            "Published": self.published,
        }

    def get_news_files(self) -> AnyStr:
        with open(self.image.absolute(), "rb") as file:
            file.read()


In the future, I take this data and pass it to my requester. How can the with open method be improved to pass an image in a post request? Is it possible to add a return?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question