Last active
June 15, 2024 17:10
-
-
Save rg3915/ab00a3789d0582c8d88cd850a05a18a6 to your computer and use it in GitHub Desktop.
Retorna Estado choices Django Ninja API TextChoices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ninja import Router, Schema | |
router = Router() | |
# models.py | |
class EstadoChoices(models.TextChoices): | |
AC = 'AC', 'Acre' | |
AL = 'AL', 'Alagoas' | |
AP = 'AP', 'Amapá' | |
AM = 'AM', 'Amazonas' | |
BA = 'BA', 'Bahia' | |
CE = 'CE', 'Ceará' | |
DF = 'DF', 'Distrito Federal' | |
ES = 'ES', 'Espírito Santo' | |
GO = 'GO', 'Goiás' | |
MA = 'MA', 'Maranhão' | |
MT = 'MT', 'Mato Grosso' | |
MS = 'MS', 'Mato Grosso do Sul' | |
MG = 'MG', 'Minas Gerais' | |
PA = 'PA', 'Pará' | |
PB = 'PB', 'Paraíba' | |
PR = 'PR', 'Paraná' | |
PE = 'PE', 'Pernambuco' | |
PI = 'PI', 'Piauí' | |
RJ = 'RJ', 'Rio de Janeiro' | |
RN = 'RN', 'Rio Grande do Norte' | |
RS = 'RS', 'Rio Grande do Sul' | |
RO = 'RO', 'Rondônia' | |
RR = 'RR', 'Roraima' | |
SC = 'SC', 'Santa Catarina' | |
SP = 'SP', 'São Paulo' | |
SE = 'SE', 'Sergipe' | |
TO = 'TO', 'Tocantins' | |
# schemas.py | |
class EstadoSchema(Schema): | |
titulo: str | |
uf: str | |
#api.py | |
@router.get('estados', response=list[EstadoSchema], tags=['Estados']) | |
def list_estados(request): | |
return [{'titulo': item[1], 'uf': item[0]} for item in EstadoChoices.choices] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment