2

I'm trying to get Union "statuses" or "ids" query parameters at my endpoint. One way to do it - create a separate schema with a single field for every query parameter (statuses, ids, etc). But this way is pretty blown up (what if I need 10 sorts of query parameters, create 10 schemas with a single field for every query parameter?)

It's possible to do it somehow else?

For example:

@router.get("", status_code=200,
            response_model=List[schemas.TicketOut],
            description="statuses or ids")
async def get_tickets(
        entity: Union[
                 List[Query(default=..., description='statuses.')],
                 List[Query(default=..., description='Ids.')]
    ],
        token: str = Depends(config.oauth2_scheme),
        postgres_session: AsyncSession = Depends(database.get_db)):
    try:
        pass

TypeError: Parameters to generic types must be types. Got Query(Ellipsis).

or

class TicketIn(TicketBase):
    ticket_id: int = Query(default=...,
                           ge=1,
                           le=10000,
                           description='Some text.',
                           example=1)

    status: TicketStatuses = Query(default=...,
                                   description='Some text',
                                   example="Some text")


@router.get("", status_code=200,
            response_model=List[schemas.TicketOut],
            description="statuses or ids.")
async def get_tickets(
        entity: Union[List[schemas.TicketIn.status], List[schemas.TicketIn.ticket_id]],
        token: str = Depends(config.oauth2_scheme),
        postgres_session: AsyncSession = Depends(database.get_db)):
    try:

AttributeError: type object 'TicketIn' has no attribute 'status'
flakes
  • 21,558
  • 8
  • 41
  • 88
salius
  • 918
  • 1
  • 14
  • 30

0 Answers0