Problem with a Choice [Django - Pyhon]

0

Good morning!

I have a modeo which has a field of type text in which is a "choice".

ANUNCIADA = 'A'
AMARRADA = 'M'
TERMINADA = 'T'
ANULADA = 'N'
LIST_ESTADOS_ANUNCIOS = (
(ANUNCIADA, 'Anunciada'),
(AMARRADA, 'Amarrada'),
(TERMINADA, 'Termina')
(ANULADA, 'Anulada'),
)

class Anuncio(models.Model):
estado_anuncio = models.CharField(max_length=1, blank=True, null=True, verbose_name='Estado', choices=LIST_ESTADOS_ANUNCIOS, default='A')

The problem I have now is to call it to the data. But I do not want with the initials, but they say 'A' = Announced, 'M' = Moored, 'T' = Finished, 'A' = Canceled. (Silly problem mine, since in the first instance you did not want to call)

If someone has an idea of how to call it that, it would be studying.

PDT: 1- In my API I call it, but as it is, it means 'A', 'M', 'T' ... and this is not required.

Thank you.

    
asked by Jeanpierre Rivas 02.02.2018 в 19:52
source

2 answers

1

A default you have to pass your class and the value you want to show, and choices also need the name of your class, assuming that your class is called ESTADOS; it would look like this:

class LIST_ESTADOS_ANUNCIOS(object):
  ANUNCIADA = 'A'
  AMARRADA = 'M'
  TERMINADA = 'T'
  ANULADA = 'N'

CHOICES_LIST_ESTADOS_ANUNCIOS = (
  (ANUNCIADA, 'Anunciada'),
  (AMARRADA, 'Amarrada'),
  (TERMINADA, 'Termina')
  (ANULADA, 'Anulada'),
)
class Anuncio(models.Model):
    estado_anuncio = models.CharField(max_length=1, blank=True, null=True, verbose_name='Estado', choices=LIST_ESTADOS_ANUNCIOS.CHOICES_LIST_ESTADOS_ANUNCIOS, default=LIST_ESTADOS_ANUNCIOS.ANUNCIADA)
    
answered by 02.02.2018 в 20:07
0

this you can do it in the following way

anuncio = Anuncio.objects.get(....)# añades los filtros que necesites
# para acceder al valor de un choice se usa get_namefield_display
# en tu caso seria get_estado_anuncio_display
print anuncio.get_estado_anuncio_display

you can use it in the same way in the templates

    
answered by 07.02.2018 в 17:52