简体   繁体   中英

Data from view to serializer in django rest api

I am struggling with this problem for a while.

Ok here is my serializer:

from rest_framework import serializers
from happytourist.models import PointsInterestData

class PointsInterestSerializer(serializers.ModelSerializer, serializers.Serializer):
    distance = serializers.IntegerField(default=None)

    class Meta:
        model = PointsInterestData
        fields = ('name', 'latitude', 'longtitude', 'distance')
        read_only_fields = fields

from rest_framework import generics
from .serializers import PointsInterestSerializer
from happytourist.models import PointsInterestData

class PointsInterestList(generics.ListCreateAPIView, generics.ListAPIView):
    serializer_class = PointsInterestSerializer


    def get_queryset(self):
        queryset = PointsInterestData.objects.all()
        return queryset

    def create(self, request, *args, **kwargs):
        user_latitude = request.POST.get('latitude')
        user_longtitude = request.POST.get('longtitude')
        radius = request.POST.get('radius')
        usergeodata = {'latitude': user_latitude, 'longtitude': user_longtitude, 'radius': radius}
        return usergeodata

    def get_coordinates(self):
        latitude = PointsInterestData.objects.model.latitude
        longtitude = PointsInterestData.objects.model.longtitude
        geodata = {"latitude": latitude, "longtitude": longtitude}
        return geodata

What I want to do is to change distance parameter in serializer according to the view. I need to write a function in view which result will be calculated distance (i know how to make this) and this result will be put into serializer (which i do not know how to make it)

If I understood correctly, you could use SerializerMethodField and you could access the data from view by self.context .

class PointsInterestSerializer(serializers.ModelSerializer):
    distance = 

all right this what I have done:

view:

class PointsInterestList(generics.ListCreateAPIView, generics.ListAPIView):

def get_queryset(self):
    queryset = PointsInterestData.objects.all()
    return queryset

def distancecalc():
    return 4

def get_serializer_context(self):
    distance_result = PointsInterestList.distancecalc()
    context = super(PointsInterestList, self).get_serializer_context()
    context.update({'distance': distance_result})
    return context

serializer_class = PointsInterestSerializer

serializer:

class PointsInterestSerializer(serializers.ModelSerializer, serializers.Serializer): distance = serializers.SerializerMethodField(default=None)

def get_distance(self, obj):
    distance_view = self.context.get('distance')
    return distance_view

class Meta:
    model = PointsInterestData
    fields = ('name', 'latitude', 'longtitude', 'distance')
    read_only_fields = fields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM