I have 3 Django models:
class Test(models.Model):
pass
class Page(models.Model):
test = models.ForeignKey(Test)
class Question(model.Model):
page = models.ForeignKey(Page)
If I register the Question
model to the admin, I get a dropdown with the desired Page
. Now, what do I have to modify to display the desired Page
plus that page's corresponding Test
?
Say, if I have three pages created, the dropdown will contain these values: Page1
, Page2
, Page3
. I would like to see: Page1 - Test1
, Page2 - Test1
, Page3 - Test1
2 Options.
Option 1:
Create a new
field
, copyforms.ModelChoiceField
and overridelabel_from_instance
.This will only change the text for that particular dropdown field. As you are accessing the
Test
object for each item in the list, you may want to ensure thequeryset
you pass to thePageModelChoiceField
hasselect_related('test')
, otherwise it will make a DB hit for each item on the list.I've not tested this exact code but the logic is there. Will try it later when I can
Option B.
Change the unicode() representation of
Page
.This will change how
Page
s are displayed everywhere you print a page object,print(page_object)
,{{ page_object }}
.Personally I prefer Option 1