django-res -framerwork序列化器如何获取用户id

时间:2022-02-23 03:48:16

a model like this:

这样的模型:

class Album(models.Model):
    artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    release_date = models.DateField()
    num_stars = models.IntegerField()

and serializer:

与序列化器:

class AlbumSerializers(serializers.ModelSerializer):
    count=serializers.SerializerMethodField()
    class Meta:
        module=Album
        fields=('__all__')

I just want know how can I get user.id in serializer

我只是想知道如何得到用户。id在序列化器

3 个解决方案

#1


1  

Django REST framework's serializers uses a context to keep those things. It is used to find out the associated hostname when using HyperlinkedSerializers and is documentated here

Django REST框架的序列化器使用上下文保存这些内容。它用于在使用HyperlinkedSerializers时查找相关主机名,并在这里记录

By default, generic view do add the current request, which means that you are able to do:

默认情况下,泛型视图确实添加了当前请求,这意味着您可以这样做:

class AlbumSerializers(serializers.ModelSerializer):

    count=serializers.SerializerMethodField()

    class Meta:
        module=Album
        fields=('__all__')

    def get_count(self,obj):
        # Provided the user is logged:
        user_id = self.context['request'].user.id
        # Now do whatever with user_id
        return

#2


0  

First thing to note here is that you must not access request other than the views. If you want to access the request then you must pass the context explicilty because django rest framework implicitly do not pass the request to serializer.

首先要注意的是,除了视图之外,您不能访问请求。如果希望访问请求,则必须传递上下文explilty,因为django rest框架隐式地不将请求传递给序列化器。

class MyAPIView(APIView):
    def get(self, request):
    query_set = MyObjects.objects.all()

    serializer = MySerializer(query_set,
                              context={'request': request},
                              many=True)
    return Response(serializer.data)

Note: This is case when you are using APIView or any other base class view. But in case of ViewSets where you don't have to override methods Django Rest Framework will automatically send the request to Serializer class. I hope it clears the concept !

注意:这是在使用APIView或任何其他基类视图时发生的情况。但是在不需要重写方法的ViewSets中,Django Rest框架会自动将请求发送到Serializer类。我希望它能澄清这个概念!

#3


0  

Serializer:

序列化器:

class AlbumSerializers(serializers.ModelSerializer):

    count=serializers.SerializerMethodField()

    class Meta:
        module=Album
        fields=('__all__')

    def get_count(self,obj):
        request = self.context.get("request")
        user_id = request.user.id

        return

#1


1  

Django REST framework's serializers uses a context to keep those things. It is used to find out the associated hostname when using HyperlinkedSerializers and is documentated here

Django REST框架的序列化器使用上下文保存这些内容。它用于在使用HyperlinkedSerializers时查找相关主机名,并在这里记录

By default, generic view do add the current request, which means that you are able to do:

默认情况下,泛型视图确实添加了当前请求,这意味着您可以这样做:

class AlbumSerializers(serializers.ModelSerializer):

    count=serializers.SerializerMethodField()

    class Meta:
        module=Album
        fields=('__all__')

    def get_count(self,obj):
        # Provided the user is logged:
        user_id = self.context['request'].user.id
        # Now do whatever with user_id
        return

#2


0  

First thing to note here is that you must not access request other than the views. If you want to access the request then you must pass the context explicilty because django rest framework implicitly do not pass the request to serializer.

首先要注意的是,除了视图之外,您不能访问请求。如果希望访问请求,则必须传递上下文explilty,因为django rest框架隐式地不将请求传递给序列化器。

class MyAPIView(APIView):
    def get(self, request):
    query_set = MyObjects.objects.all()

    serializer = MySerializer(query_set,
                              context={'request': request},
                              many=True)
    return Response(serializer.data)

Note: This is case when you are using APIView or any other base class view. But in case of ViewSets where you don't have to override methods Django Rest Framework will automatically send the request to Serializer class. I hope it clears the concept !

注意:这是在使用APIView或任何其他基类视图时发生的情况。但是在不需要重写方法的ViewSets中,Django Rest框架会自动将请求发送到Serializer类。我希望它能澄清这个概念!

#3


0  

Serializer:

序列化器:

class AlbumSerializers(serializers.ModelSerializer):

    count=serializers.SerializerMethodField()

    class Meta:
        module=Album
        fields=('__all__')

    def get_count(self,obj):
        request = self.context.get("request")
        user_id = request.user.id

        return