【问题标题】:Serialize GeoQuerySet as JSON in Django REST?在 Django REST 中将 GeoQuerySet 序列化为 JSON?
【发布时间】:2015-03-06 23:16:32
【问题描述】:

我正在尝试使用 Django REST 框架将 GeoQuerySet 序列化为 JSON。我是 Django 新手,一般对数据库知之甚少,因此我们将不胜感激。

我已经尝试解决这个问题一整天了。

这是我想要做的:

class PointView(generics.ListAPIView):
    serializer_class = MyEntityModelSerializer

    def post(self, request, *args, **kwargs):
        """
        Enter description
        here
        """

        entity=EntityType.objects.exclude(point=None)[0]
        lon=request.data['lng']
        lat=request.data['lat']
        radius_km=request.data['radius_km']
        within_radius=entity.is_near(lat,lon,radius_km)

        return within_radius

这是我的错误信息:

AssertionError at /point/

Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'django.contrib.gis.db.models.query.GeoQuerySet'>`

【问题讨论】:

    标签: json django rest geo


    【解决方案1】:

    解决方案!原来 POST 是为更新数据而设计的,而我只是想获取数据。

    Django REST 框架 (DRF) 是按照标准构建的,因此我试图强制执行的行为并不是该框架旨在执行的操作。

    这是对我有用的解决方案:

    class PointView(generics.ListAPIView):
        serializer_class = MyEntityModelSerializer
    
        def get_queryset(self):
           """
            Enter description
            here
            """
    
            params = self.request.query_params
    
            entity=EntityType.objects.exclude(point=None)[0]
            lon=float(params['lng'])
            lat=float(params['lat'])
            radius_km=float(params['radius_km'])
            within_radius=entity.is_near(lat, lon, radius_km)
    
            return within_radius
    

    感谢 irc.freenode.net/django 上的 bibhas 和 tbaxter,他们指出了我的想法中的错误:GET-Styled POST-Requests are so not HTTP/1.1

    【讨论】:

      猜你喜欢
      • 2018-10-27
      • 1970-01-01
      • 2015-10-29
      • 2016-05-06
      • 2010-09-29
      • 2013-05-23
      • 2016-04-12
      • 1970-01-01
      • 2011-11-30
      相关资源
      最近更新 更多