Django Crispy Multichoices Form (Foreignkey) is printing "Object" instead of Object.name

Viewed 1137

I have this model.py

class BD(models.Model):
    b_Name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.b_Name


class Data(models.Model):
    o_name = models.CharField(verbose_name='Enter your Name', max_length=100)
    d_name = models.CharField(max_length=100)
    b = models.ForeignKey(BD)

And I have this form.py :

from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field
from crispy_forms.bootstrap import (
    PrependedText, PrependedAppendedText, FormActions)

from .models import Data, BD


class Form(forms.ModelForm):
    class Meta:
        model = Data
        fields = ('o_name', 'd_name', 'b')
    helper = FormHelper()
    helper.form_method = 'POST'
    helper.add_input(Submit('Submit', 'Submit', css_class='btn-primary'))

The problem lies with b. So if I understand correctly how this works, the foreign key makes django get some object contained in a table called "data_DB" on my postgresql server.
What I don't get is that, for b, crispy should generate a listbox containing the b_name of each row of my postgresql table whereas it only displays "BD object" as a choice, as much time as there are rows in my table.

I thought the function :

def __unicode__(self):
        return self.b_Name

was meant to return what i was interested in the object instead of the object itself.

What did I misunderstand ?

EDIT : that's the HTML output :

<select class="select form-control" id="id_b" name="b">
<option value="" selected="selected">---------</option>
<option value="2">BData object</option>
<option value="1">BData object</option>
<option value="3">BData object</option>
</select>

Value should be BDdate.b_Name

1 Answers
Related