/ / Dostosowywanie rozwijanych wartości z wartościami modelu rodzica w administratorze Django - django, django-models, django-forms, django-admin

Dostosowywanie rozwijanych wartości z wartościami modelu rodzica w administratorze Django - django, django-models, django-forms, django-admin

Mam kilka modeli w 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

Teraz, kiedy próbuję dodać nowy rekord dla Third model w moim administratorze Django, mój <select> pole jest wypełnione second_name wartości. Jak wyświetlić połączony ciąg różnych wartości z obu "nadrzędnych" tabel, np. first_name + first_value + second_name + second_value?

Odpowiedzi:

1 dla odpowiedzi № 1

Po prostu zaktualizuj unicode metoda second Model:

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)