我想显示与其对应的每个问题页面我的课程页面中有4个项目(“访问1访问2访问3访问4”)
现在我似乎立刻得到了所有的页面...我想问一个问题,例如“你多大了?”这是在管理面板“仅访问1”
中此次访问1定义的我的models.py:
class Page(models.Model):
title = models.CharField(max_length=30)
def __str__(self):
return self.title
class Question(models.Model):
label = models.CharField(max_length=30)
page = models.ManyToManyField(Page)
def __str__(self):
return self.label
class Reply(models.Model):
question = models.ForeignKey(Question)
user = models.ForeignKey(Personne)
answer = models.CharField(max_length=30)
creationDate = models.DateTimeField(default=datetime.datetime(2016, 1, 20, 15, 4, 21, 467165))
def __str__(self):
return str(self.answer)
我的admin.py:
class ReplyAdmin(admin.ModelAdmin):
list_display = ["__str__", "user", "question", "creationDate", "get_pages",]
def get_pages(self, objects):
return "\n".join([page.title for page in Page.objects.all()])
class Meta:
model = Reply
admin.site.register(Reply, ReplyAdmin)
为了获得每个关联网页的正确行,我必须面对的循环是什么
答案 0 :(得分:2)
class ReplyAdmin(admin.ModelAdmin):
list_display = ["__str__", "user", "question", "creationDate", "get_pages",]
def get_pages(self, obj):
return "\n".join([page.title for page in obj.question.page.all()])
class Meta:
model = Reply
admin.site.register(Reply, ReplyAdmin)