【发布时间】:2018-07-29 09:27:36
【问题描述】:
我没有找到这方面的信息。而且我不确定是否可以这样做。所以我关注Page 模型及其关系:
class TeamRooster(Page):
team_logo = models.ForeignKey(
'wagtailimages.Image',
null=True, blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
@register_snippet
class GroupstageTournamentModel(ClusterableModel):
team_1 = models.ForeignKey(
TeamRooster,
null=True, verbose_name='Erste Team',
on_delete=models.SET_NULL,
related_name="+",
)
class GroupstageScreencastRelationship(Orderable, models.Model):
page = ParentalKey('ScreencastPage',
related_name='groupstage_screencast_relationship')
match = models.ForeignKey('GroupstageTournamentModel',
related_name='match_screen_relationship')
panels = [
SnippetChooserPanel('match')
]
class ScreencastPage(Page):
content_panels = Page.content_panels + [
InlinePanel(
'groupstage_screencast_relationship', label="Choose Teams"
]
def matches(self):
matches = [
n.match for n in self.groupstage_screencast_relationship.all()
]
return matches
def serve(self, request):
if request.is_ajax():
result = [
{
'team_1_logo': match.team_1.team_logo
}
for match in self.matches()
]
json_output = json.dumps(result)
return HttpResponse(json_output)
else:
return super(ScreencastPage, self).serve(request)
是否可以通过 ajax 请求从TeamRooster 模型中获取图片?
如果我尝试按照上面的代码所示执行此操作,则会收到错误消息:TypeError: Object of type 'Image' is not JSON serializable
【问题讨论】: