Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
django many-to-many show human-readable in form

I have these models:

CURSES=(('python','Python'),('django','Django'),...)
class Asig(models.Model):
    ...
    name = models.CharField(max_length=100, choices=CURSES)

class Profesor(AbstractUser):
    ...
    asigs = models.ManyToManyField(Asig)

Then, when I render the form using ModelForm the many-to-many field shows itself with 'python' string instead 'Python', also, when I look the rendered html coded the multiselect options look like:

<option value='1'>python</option>

instead of

<option value='python'>Python</option> 
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If you want to use the value 'Python' in the model's __str__, method, then you should use self.get_name_display() instead of self.name:

class Asig(models.Model):
    ...
    name = models.CharField(max_length=100, choices=CURSES)

    def __str__(self):  
        # use @python_2_unicode_compatible or define __unicode__ if using Python 2
        return self.get_name_display()

You can't easily change many-to-many field to use value='python' instead of value='1' (the primary key). That's just the way many-to-many fields work.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda