[Python] How to access model method in enumerate of list in Python?

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm new to python and I've managed to populate a list with instances of my model.

    # requisition.py

    from pydantic import BaseModel
    from app.schemas.job_information import JobInformation

    class Requisition(BaseModel):
    # Base fields (common between internal and external)
    index: int = 0
    contest_number: int = 0
    open_date: str = None
    close_date: str = ""
    job_information: JobInformation = None

    def set_index(self, index):
    self.index = index

    def set_contest_number(self, contest_number):
    self.contest_number = contest_number

    def set_open_date(self, open_date):
    self.open_date = open_date

    def set_close_date(self, close_date):
    self.close_date = close_date

    def set_job_information(self, job_information):
    self.job_information = job_information


    Then in my main.py

    @router.get("/")
    async def get_sourcing_requests():
    requisitions = []
    pageindex = 1
    pagecount, reqs = get_sourcing_requests_by_page_index(pageindex)

    requisitions.append(reqs)

    while pageindex < pagecount:
    pageindex += 1
    pagecount, reqs = get_sourcing_requests_by_page_index(pageindex)
    requisitions.append(reqs)

    for i, req in enumerate(requisitions):
    req.set_index(int(i))

    return requisitions


    But I keep getting the error


    AttributeError: 'list' object has no attribute 'set_index'
    ​

    I'm guessing req is considered a list object instead of a Requisition object but not really sure.

    Appreciate any help.

    Using Python 3.13.3.

    Continue reading...

Compartilhe esta Página