gpt4 book ai didi

python - 从 ManyToMany 在 admin.py 中添加字段

转载 作者:行者123 更新时间:2023-11-28 17:29:48 25 4
gpt4 key购买 nike

我想为每个与其对应的问题页面显示我的类(class)页面中有 4 个项目(“访问 1 访问 2 访问 3 访问 4”)

现在我似乎一次获得所有页面......我想要例如“你多大了?”这个问题。仅在管理面板“访问 1”中用访问 1 定义

My 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)

My 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)

为每个关联页面获取正确的行我必须面对的循环是什么

最佳答案

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)

关于python - 从 ManyToMany 在 admin.py 中添加字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35225712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com