我有那么一天,我认为这个项目上的事情会很容易,但结果却并非如此.我觉得在我的挫败感中,我一定忽略了一些非常简单的东西.

我有一个API观,它传递一个UUID来标识游戏故事的"章节",判断当前登录的用户是否有条目表明他们完成了挑战,如果有,则返回下一个Hunt线索(与章节挑战不同)或0,供我在UI中处理.

由于某些原因,它不会在查询中返回任何结果.然而,当我输出原始的SQL查询(queryset.ery)时,该查询返回预期的结果.

任何帮助都不胜感激.

class GetHuntClue(API观):

    def get(self, request, **kwargs):
        serializer_context = {
            'request': request,
        }
        chapter_uuid = self.kwargs['chapter_uuid']
        curr_user = self.request.user
        #check to make sure it's answered then provide the appropriate hunt clue.  
        log_type = GameActivityType.objects.filter(activity_type_name="Correct Challenge Answer").first()
        already_ans = GameActivity.objects.filter(user=curr_user).filter(activity_chapter=chapter_uuid).filter(activity_type=log_type).count()
        if (already_ans):
            queryset = HuntClue.objects.filter(hunt_chapter=chapter_uuid)
            print(queryset.query)
            serializer = HuntClue串行器(queryset, context=serializer_context)
            return Response(serializer.data)            
        else:
            return HttpResponse(already_ans, content_type="text/plain")

串行器

class HuntClue串行器(serializers.模型串行器):

    class Meta:
        model = HuntClue
        #fields = '__all__'
        fields = ("id", "hunt_title", "hunt_clue", "hunt_chapter")

模型

class HuntClue(models.模型):
    hunt_title = models.CharField(max_length=100)
    hunt_clue = models.CharField(max_length=500)
    hunt_chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE, null=True, blank=True, related_name="chapter_hunt_clue")

    def __str__(self):
        return '%s' % (self.hunt_title)

原始查询:

SELECT "simgame_huntclue"."id", "simgame_huntclue"."hunt_title", "simgame_huntclue"."hunt_clue", "simgame_huntclue"."hunt_chapter_id" FROM "simgame_huntclue" WHERE "simgame_huntclue"."hunt_chapter_id" = f7ff3135-9212-47b1-a4f3-0d920c01c020

id |    hunt_title     |                 hunt_clue                  |           hunt_chapter_id            
----+-------------------+--------------------------------------------+--------------------------------------
  1 | Getting warmer... | Find the QR Code at the front of the room. | f7ff3135-9212-47b1-a4f3-0d920c01c020
(1 row)

但在视图中,我得到了一个错误:

AttributeError at /api/v1/gethuntclue/f7ff3135-9212-47b1-a4f3-0d920c01c020

Got AttributeError when attempting to get a value for field `hunt_title` on serializer `HuntClue串行器`.
The serializer field might be named incorrectly and not match any attribute or key on the `QuerySet` instance.
Original exception text was: 'QuerySet' object has no attribute 'hunt_title'.

对此感到非常困惑……

感谢任何洞察力.

BCBB

推荐答案

你应该加many=True.因为你设定的是queryset,而不是object.

serializer = HuntClueSerializer(queryset, context=serializer_context, many=True)

Django相关问答推荐

django命令中的no_color不起作用

Django ORM Groupby

将所有守护用户对象权限从一个Django用户重新分配给另一个Django用户

基于Django类的视图:除非登录,否则拒绝访问future 日期

通过get_form_kwargs将请求传递给Django表单未能使表单访问self.request.user.

身份验证有效,但登录无效.一直卡在pending

NoneType对象没有属性保存Django

在 Django Rest Framework 视图中,request.user 是在请求​​到达视图之前进行数据库调用还是数据库调用发生?

Django过滤器查询外键

使用 Vagrant 进行端口转发时连接重置

有没有办法将变量传递给 Django 中的 extended模板?

模型 Django 中的 ID 字段

如何在 Django Rest Framework SimpleRouter 上使斜杠可选

如何在 Django 和 django-jsonfield 中将 JSONField 的默认值设置为空列表?

用于创建和更新的基于 Django 类的视图

Django将整数模型字段的范围设置为约束

django rest 框架:从序列化程序 validate() 方法设置字段级错误

Django中'related_name'和'related_query_name'属性之间的区别?

获取'str'对象在Django中没有属性'get'

判断列表 django 模板中是否存在