2013-01-11 77 views
1

我有幾個模型在Django:自定義下拉與父母的模型值值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 

現在,當我嘗試在我的Django管理增添了新的紀錄Third模型,我<select>盒填入second_name值。如何顯示來自「父」表的不同值的連接字符串,例如first_name + first_value + second_name + second_value

回答

1

只需更新second型號的unicode方法:

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) 
+0

啊,對吧=)簡單一如既往使用Django =)非常感謝! – errata