开发者

problem with select and related model

开发者 https://www.devze.com 2022-12-21 12:28 出处:网络
I have models like this: class IdfPracownicy(models.Model): nazwa = models.CharField(max_length=100) class IdfPracaOpinie(models.Model):

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()开发者_StackOverflow中文版,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?


Define a __unicode__ methods for your models:

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

       def __unicode__(self):
            return self.nazwa


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
0

精彩评论

暂无评论...
验证码 换一张
取 消