Answer the question
In order to leave comments, you need to log in
How to display data in list_display from another table?
Hello, I need your help. Let's say there is model data
models.py
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
class QuestionAdmin(admin.ModelAdmin):
list_display = ('question_text', 'pub_date', 'was_published_recently')
class Choice(admin.ModelAdmin):
list_display = ('question, 'choice_text', 'votes')
Answer the question
In order to leave comments, you need to log in
'question__was_published_recently'
if not then just define it in the method use the name
def get_was_published_recently(self, obj):
if obj.question:
return obj.question.was_published_recently
return '-'
get_was_published_recently.short_description = (_('published recently'))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question