2 options.
Option 1:
Create a new field , copy forms.ModelChoiceField and override label_from_instance .
This will only change the text for this particular dropdown field. As you access the Test object for each item in the list, you can provide the queryset you pass. PageModelChoiceField has select_related('test') , otherwise it will do a DB hit for each item in the list.
I did not check this exact code, but there is logic. I'll try later when I can
class QuestionForm(forms.ModelForm): page = PageModelChoiceField( queryset=Page.objects.select_related('test').all() ) class Meta: model = Page class QuestionAdmin(ModelAdmin): class Meta: model = Question form = QuestionForm
Option B.
Change the Unicode () Page view.
class Page(models.Model): test = models.ForeignKey(Test) def __unicode__(self): return "Page{0} - Test{1}".format(obj.pk, obj.test.pk)
This will change the way the Page displayed wherever you print the page object: print(page_object) , {{ page_object }} .
Personally, I prefer option 1
rockingskier
source share