[Answered ]-Displaying a combobox for a OneToOneField

2👍

You don’t need to construct your combobox manually. It will automatically be created by Django. Just use

<form id="myForm" action="" method="post">
    {% csrf_token %}
    {{ form_aliment }}
    <button type="submit">Submit</button>
</select>

as the basis for your template.

You also have to implement the __unicode__ method in your class to see their names in the combo box:

class type_aliment(models.Model):
    ...
    def __unicode__(self):
        return self.name

PS: Your naming convention is confusing. Try to stick with Python/Django standards. Use CamelCase for class names; for example, instead of

class type_aliment(...)

use

class TypeAliment(...)

and don’t add the _id suffix to your field names. Instead of

type_aliment_id = models.OneToOneField(type_aliment)

use

type_aliment = models.OneToOneField(TypeAliment)

it will help fellow coders (like here on Stack Overflow) read your code more easily.

👤Selcuk

Leave a comment