1

我在 Django 中有几个模型:

from django.db import models

class First(models.Model):
    first_name = models.CharField('First Name', max_length=100)
    first_value = models.IntegerField('Value')

    def __unicode__(self):
        return self.first_name 

class Second(models.Model):
    first_ref = models.ForeignKey(First)
    second_name = models.CharField('Second Name', max_length=100)
    second_value = models.IntegerField('Second Value')

    def __unicode__(self):
        return self.second_name

class Third(models.Model):
    second_ref = models.ForeignKey(Second)
    third_name = models.CharField('Third Name', max_length=100)
    third_value = models.IntegerField('Third Value')

    def __unicode__(self):
        return self.third_name

现在,当我尝试Third在我的 Django 管理员中为模型添加新记录时,我的<select>框中填充了second_name值。如何显示两个“父”表中不同值的串联字符串,例如first_name + first_value + second_name + second_value

4

1 回答 1

1

只需更新模型的unicode方法second

class Second(models.Model):
    first_ref = models.ForeignKey(First)
    second_name = models.CharField('Second Name', max_length=100)
    second_value = models.IntegerField('Second Value')

    def __unicode__(self):
        return '%s %s %s %s' % (
            self.first_ref.first_name, self.first_ref.first_value,
            self.second_name, self.second_value)
于 2013-01-11T23:38:18.713 回答