Linked Questions

Popular Questions

Use orjson/ujsonin drf

Asked by At

Hi all im looking into faster ways of returning JSON data etc. I know that FastAPI uses


Is there a way to replace the standard drf serializer to orjson?

edit Lets say i have this viewset

class ProfileViewSet(viewsets.ModelViewSet):
    permission_classes = [ProfilePermission]
    serializer_class = ProfileSerializer
    pagination_class = ProfilePagination
    filterset_class  = ProfileFilter

    def get_queryset(self):

        current_id =
        blocked_users = Blocked.objects.filter(user__id=current_id).values('blocked')

        users_taken_action = Action.objects.filter(user__id=current_id).values('action_user')

        users_not_taken_action = Profile.objects.exclude(user__in=users_taken_action) \

        return users_not_taken_action

and this serializer

class ProfileSerializer(WritableNestedModelSerializer, serializers.ModelSerializer):
    fav_data = serializers.SerializerMethodField(read_only=True)
    images = serializers.SerializerMethodField(read_only=True)
    user = UserSerializer(read_only=True)
    place = PlaceSerializer(read_only=True)

    def get_images(self, obj):
        return PhotoSerializer(UserPhoto.objects.filter(, many=True).data

    def get_fav_data(self, obj):
        fav_data = Favorite.objects.get(
        return {
            "category": fav_data.category,

    class Meta:
        model = Profile
        fields = '__all__'
        depth = 1

How can I convert it to ojson/urjson

Related Questions