views:

31

answers:

2

I have models like this:

class IdfPracownicy(models.Model):
   nazwa = models.CharField(max_length=100)

class IdfPracaOpinie(models.Model):
   nazwa = models.CharField(max_length=30)

class IdfPraca(models.Model):
    numer_idf = models.ForeignKey(IdfPracownicy)
    [...]
    opinia = models.ForeignKey(IdfPracaOpinie)
    uwagi = models.TextField()
    data_wpisu = models.DateTimeField(auto_now=True)

I create my form like this:

class IdfPracaForm(forms.ModelForm):
    numer_idf = forms.ModelChoiceField(queryset=IdfPracownicy.objects.all(), widget=forms.HiddenInput)
    opinia = forms.ModelChoiceField(queryset=IdfPracaOpinie.objects.all(),empty_label=None)

    class Meta:
        # Nazwa modelu
        model = IdfPraca

When I diplay this form in my browser, the <option> tag shows an object instance like this:

<select name="opinia" id="id_opinia"> 
<option value="1">IdfPracaOpinie object</option> 
<option value="2">IdfPracaOpinie object</option> 
<option value="3">IdfPracaOpinie object</option> 
<option value="4">IdfPracaOpinie object</option> 
</select>

Instead of field "nazwa" values. What am I doing wrong?

+1  A: 

Implement a __unicode__ method on your IdfPracaOpinie model, like this:

class IdfPracaOpinie(models.Model):
   nazwa = models.CharField(max_length=30)

   def __unicode__(self):
      return self.nazwa
Dominic Rodger
+1  A: 

Define a __unicode__ methods for your models:

class IdfPracownicy(models.Model):
       nazwa = models.CharField(max_length=100)

       def __unicode__(self):
            return self.nazwa
Silver Light
thank you so much :)
manner
@manner - if Konstantin's answer worked for you, then it's helpful for you to upvote it and mark it as accepted (click the check mark underneath the 0 by his answer).
Dominic Rodger
He needs 15 reputation to upvote and has only 3 now
Silver Light