我一直在尝试输出获得作者姓名和书名的结果是同一个json dict,我得到了作者写的所有书籍,但我也想在书籍之前找到作者姓名,这里是相关代码:< / p>
模型
class Books(models.Model):
bid = models.BigIntegerField(primary_key=True)
bname = models.CharField(max_length=200, blank=True, null=True)
bdescription = models.TextField(blank=True, null=True)
def __str__(self):
return self.bname
class Authors(models.Model):
aid = models.AutoField(primary_key=True)
aname = models.CharField(max_length=200, blank=True, null=True)
adescription = models.TextField( blank=True, null=True)
def __str__(self):
return self.aname+"\n"
class Bookauth(models.Model):
bid = models.ForeignKey(Books, on_delete=models.DO_NOTHING, db_column='bid', blank=True, null=True)
aid = models.ForeignKey(Authors, on_delete=models.DO_NOTHING, db_column='aid', blank=True, null=True)
视图
def authbook(request):
l = request.GET.get('bks', '')
r = Books.objects.filter(bookauth__aid = l).values('bname')
s = Authors.objects.filter(aid = l).values('aname')
return JsonResponse({s: list(r)})
这段代码显然给出了关于s不是字符串的错误。