Show specific attribute in Select using Python and Django

Asked

Viewed 85 times

0

In models.py created the following tables:

class UsuarioAdicionalPlano(models.Model):
    quantidade = models.PositiveIntegerField(primary_key=True)
    preco = models.DecimalField(decimal_places=2, max_digits=13)

class ConfiguracoesDoPlano(models.Model):
    plano = models.ForeignKey('login.Plano', related_name='plano', on_delete=models.CASCADE)
    usuarios_adicional = models.ForeignKey('login.UsuarioAdicionalPlano', related_name='usuario_adicional', on_delete=models.CASCADE)

In forms.py

class ConfiguracoesDoPlanoForm(forms.ModelForm):
    class Meta:
        model = ConfiguracoesDoPlano
        fields = ('plano', 'notas_adicional', 'usuarios_adicional')
        widgets = {
            'plano': forms.Select(attrs={'class': 'form-control'}),
            'usuarios_adicional': forms.Select(attrs={'class': 'form-control'}),
    }
        labels = {
            'plano': 'Plano escolhido',
            'usuarios_adicional': 'Usuários adicionais',
    }

And in the archive .html I have the following Select The form comes from ConfiguracoesDoPlanoForm table ConfiguracoesDoPlano

<div class="form-group">
    <label>{{form.usuarios_adicional.label}}</label><strong style="color:red;"> *</strong>
       {% if form.usuarios_adicional.errors %}<label class="error">{% for error in form.usuarios_adicional.errors %}{{error}}{% endfor %}</label>{% endif %}
       <div class="input-group" style="margin-bottom: 0px;">
           <div class="form-line">
           {{form.usuarios_adicional}}
           </div>
       </div>
</div>

But mine select is appearing with the values 'UsuarioAdicionalPlano object (0)' , 'UsuarioAdicionalPlano object (1)', and so on.

Como aparece o meu select

How do I make the values of the Select is the value of the attribute quantidade?

1 answer

1


this is not related to function __str__ class? Try declaring the function __str__ returning the output you want in the model Useraddilplano, something like this:

def __str__():
   return this.quantidade
  • 1

    Worked using the function def __str__(self): return str(self.quantidade)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.