M
M
Mike2018-10-29 15:38:12
Django
Mike, 2018-10-29 15:38:12

How to get the fields of the current user?

I have a model.

class UserLevel(models.Model):

    level = models.ForeignKey(Level, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    progress = models.IntegerField(default=0)

I am trying to get these fields in views.py like this
class UserProgressList(APIView):

    def get(self, request, format=None):
        queryset_ = UserLevel.objects.filter(user_id=request.user.id)
        # queryset_ = UserLevel.objects.filter(user_id=request.user.id).values_list('progress', flat=True)
        serializer = UserLevelSerializer(queryset_)
        return Response(serializer.data)

but I get
Original exception text was: 'QuerySet' object has no attribute 'level'.

How can I get them?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Leonid, 2018-10-29
@google_online

serializer = UserLevelSerializer(queryset_, many=True)

Also in the queryset you should do select_related to avoid a bunch of unnecessary queries to the database

S
Sergey Gornostaev, 2018-10-29
@sergey-gornostaev

filterreturns not a model object, but QuerySetobjects. Useget

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question