Skip to content

Instantly share code, notes, and snippets.

@zokis
Last active April 5, 2019 18:05
Show Gist options
  • Save zokis/77eb1c8201013da184a664147dab4587 to your computer and use it in GitHub Desktop.
Save zokis/77eb1c8201013da184a664147dab4587 to your computer and use it in GitHub Desktop.
Exemplo de padrão de Choices para Django
# Modelo
class ProjetoPagamento(models.Model):
class TIPOS_PAGAMENTOS(object):
PANTIO = 1
MANUTENCAO = 2
PLANTIO_ADICIONAL = 3
MANUTENCAO_ADICIONAL = 4
CHOICES = (
(PANTIO, 'Plantio'),
(MANUTENCAO, 'Manutenção'),
(PLANTIO_ADICIONAL, 'Plantio Adicional'),
(MANUTENCAO_ADICIONAL, 'Manutenção Adicional')
)
tipo = models.IntegerField(choices=TIPOS_PAGAMENTOS.CHOICES, null=True, blank=True)
valor = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True)
# exemplo de Uso
proj_pag = ProjetoPagamento.objects.get(pk=1)
if proj_pag.tipo == ProjetoPagamento.TIPOS_PAGAMENTOS.MANUTENCAO:
proj_pag.valor *= 2
proj_pag.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment