I am having one of those days where the things I think are going to be easy on this project are just not turning out that way. I feel like I must be overlooking something very simple here in my frustration...
I have an APIView that passes in the a UUID to identify the "chapter" of the game story, checks to see if the current logged in user has an entry saying they completed the challenge, then if so returns the next Hunt Clue (different from the chapter challenge) or zero for me to handle in the UI.
For some reason, it is not returning any results in the query. However, when I output the raw sql query (queryset.query) the query returns the results expected just fine.
Any help greatly appreciated.
View
class GetHuntClue(APIView):
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 = HuntClueSerializer(queryset, context=serializer_context)
return Response(serializer.data)
else:
return HttpResponse(already_ans, content_type="text/plain")
Serializer
class HuntClueSerializer(serializers.ModelSerializer):
class Meta:
model = HuntClue
#fields = '__all__'
fields = ("id", "hunt_title", "hunt_clue", "hunt_chapter")
Model
class HuntClue(models.Model):
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)
Raw Query:
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)
But in the view I get an error:
AttributeError at /api/v1/gethuntclue/f7ff3135-9212-47b1-a4f3-0d920c01c020
Got AttributeError when attempting to get a value for field `hunt_title` on serializer `HuntClueSerializer`.
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'.
Very confused by this....
Thanks for any insight.
BCBB
You should add many=True
. Because you set queryset
not object
.
serializer = HuntClueSerializer(queryset, context=serializer_context, many=True)