在Django GeoJSON序列化程序字段中指定相关型号的字段

Specify fields of related model in Django GeoJSON serializer field(在Django GeoJSON序列化程序字段中指定相关型号的字段)

本文介绍了在Django GeoJSON序列化程序字段中指定相关型号的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Geojson序列化程序在地图中绘制LatLong点。对于此功能,我有两个名为ActivityClusterA的模型。

活动是存储项目中定义的某些活动的数据的模型。此活动包含名为locationPointField字段。 这是我的Activity模型:

class Activity(models.Model):
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=500)
    target_number = models.IntegerField(null=True, blank=True)
    target_unit = models.CharField(max_length=200, null=True, blank=True)
    beneficiary_level = models.BooleanField(default=True)
    weight = models.FloatField(default=0)
    location = PointField(geography=True, srid=4326, blank=True, null=True)

    def __str__(self):
        return self.name

    @property
    def latitude(self):
        if self.location:
            return self.location.y

    @property
    def longitude(self):
        if self.location:
            return self.location.x
类似地,一个活动可以属于一个集群。此数据存储在模型ClusterA(群集活动)中。ClusterA是指特定于某个群集的活动。

集群模型

class Cluster(models.Model):
    name = models.CharField(max_length=200)
    ward = models.CharField(max_length=200)

    def __str__(self):
        return self.name

ClusterA模型

class ClusterA(models.Model):
    activity = models.ForeignKey('Activity', related_name='clustera')
    target_number = models.IntegerField(null=True, blank=True, default=0)
    target_unit = models.CharField(max_length=200, null=True, blank=True, default='')
    time_interval = models.ForeignKey(ProjectTimeInterval, related_name='cainterval', null=True, blank=True)
    target_completed = models.IntegerField(null=True, blank=True, default=0)
    interval_updated = models.BooleanField(default=False)
    target_updated = models.BooleanField(default=False)
    location = PointField(geography=True, srid=4326, blank=True, null=True)

    def __str__(self):
        return self.name

    @property
    def latitude(self):
        if self.location:
            return self.location.y

    @property
    def longitude(self):
        if self.location:
            return self.location.x

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        if not self.id:
            if not self.activity.beneficiary_level:
                self.target_unit = self.activity.target_unit
            self.time_interval = self.activity.time_interval
        return super(ClusterA, self).save()

现在,我使用一个函数返回集群活动的Geojson数据,如下所示:

def get_map_data(request):
    ca = ClusterA.objects.all()
    data = serialize(
        'geojson',
        ca,
        geometry_field='location',
        fields = ('activity', 'location', )
    )
    print(data)
    return HttpResponse(data)

我得到的输出是:

{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:4326"}}, "features": [{"geometry": {"type": "Point", "coordinates": [85.336775, 27.542718]}, "type": "Feature", "properties": {"activity": 27}}, {"geometry": null, "type": "Feature", "properties": {"activity": 19}}, {"geometry": {"type": "Point", "coordinates": [85.336776, 27.735227]}, "type": "Feature", "properties": {"activity": 26}}]}

活动字段提供活动的ID。但我需要活动名称,以便我可以在地图中绘制的标记的弹出窗口中显示活动名称

这是我尝试在标记弹出窗口中显示活动名称的方式:

onEachFeature: function (feature, layer) {
   layer.bindPopup(feature.properties.name);
} 

如果我将本地模型的字段传递给弹出窗口,则弹出窗口显示其他数据。

我已尝试使用:

fields = ('activity__name', 'location', )

get_map_data函数中,但它不会在打印输出中将该字段显示为:

{
  "type": "FeatureCollection",
  "crs": {
    "type": "name",
    "properties": {
      "name": "EPSG:4326"
    }
  },
  "features": [
    {
      "geometry": {
        "type": "Point",
        "coordinates": [
          85.336775,
          27.542718
        ]
      },
      "type": "Feature",
      "properties": {

      }
    },
    {
      "geometry": null,
      "type": "Feature",
      "properties": {

      }
    },
    {
      "geometry": {
        "type": "Point",
        "coordinates": [
          85.336776,
          27.735227
        ]
      },
      "type": "Feature",
      "properties": {

      }
    }
  ]
}

如您所见,在上述输出的properties中没有指定任何字段。

我需要的帮助是能够获取活动模型的名称字段,而不是ID。

我正在使用Django 1.8

编辑: 添加select_related后的打印输出(例如dict)

{'activity_id': 44, 
'target_unit': u'Check', 
'_state': <django.db.models.base.ModelState object at 0x7f57e19c8150>, 
'target_completed': 0, 
'cag_id': 35, 
'target_updated': False, 
'_activity_cache': <Activity: Test>, 
'location': <Point object at 0x7f57e19391c0>, 
'time_interval_id': 84, 
'target_number': 12, 
'interval_updated': False, 
'id': 72}

自定义序列化程序的错误回溯

ERROR 2019-06-12 14:40:15,638 base 27641 140154705491712 Internal Server Error: /core/get-map-data/
Traceback (most recent call last):
  File "/home/sanip/.virtualenvs/mes/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/sanip/naxa/mes-core/onadata/apps/core/views.py", line 366, in get_map_data
    data = serializers.serialize(ca, geometry_field='location', fields=('activity__name', 'location',))
  File "/home/sanip/.virtualenvs/mes/lib/python2.7/site-packages/django/core/serializers/base.py", line 69, in serialize
    self.end_object(obj)
  File "/home/sanip/naxa/mes-core/onadata/apps/core/serializers.py", line 170, in end_object
    super(CustomSerializer, self).end_object(obj)
  File "/home/sanip/.virtualenvs/mes/lib/python2.7/site-packages/django/core/serializers/json.py", line 61, in end_object
    cls=DjangoJSONEncoder, **self.json_kwargs)
  File "/usr/lib/python2.7/json/__init__.py", line 189, in dump
    for chunk in iterable:
  File "/usr/lib/python2.7/json/encoder.py", line 434, in _iterencode
    for chunk in _iterencode_dict(o, _current_indent_level):
  File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
    for chunk in chunks:
  File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
    for chunk in chunks:
  File "/usr/lib/python2.7/json/encoder.py", line 442, in _iterencode
    o = _default(o)
  File "/home/sanip/.virtualenvs/mes/lib/python2.7/site-packages/django/core/serializers/json.py", line 115, in default
    return super(DjangoJSONEncoder, self).default(o)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <Point object at 0x7f783f0c92e0> is not JSON serializable
Internal Server Error: /core/get-map-data/
Traceback (most recent call last):
  File "/home/sanip/.virtualenvs/mes/lib/python2.7/site-packages/django/core/handlers/base.py", line 132, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/sanip/naxa/mes-core/onadata/apps/core/views.py", line 366, in get_map_data
    data = serializers.serialize(ca, geometry_field='location', fields=('activity__name', 'location',))
  File "/home/sanip/.virtualenvs/mes/lib/python2.7/site-packages/django/core/serializers/base.py", line 69, in serialize
    self.end_object(obj)
  File "/home/sanip/naxa/mes-core/onadata/apps/core/serializers.py", line 170, in end_object
    super(CustomSerializer, self).end_object(obj)
  File "/home/sanip/.virtualenvs/mes/lib/python2.7/site-packages/django/core/serializers/json.py", line 61, in end_object
    cls=DjangoJSONEncoder, **self.json_kwargs)
  File "/usr/lib/python2.7/json/__init__.py", line 189, in dump
    for chunk in iterable:
  File "/usr/lib/python2.7/json/encoder.py", line 434, in _iterencode
    for chunk in _iterencode_dict(o, _current_indent_level):
  File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
    for chunk in chunks:
  File "/usr/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict
    for chunk in chunks:
  File "/usr/lib/python2.7/json/encoder.py", line 442, in _iterencode
    o = _default(o)
  File "/home/sanip/.virtualenvs/mes/lib/python2.7/site-packages/django/core/serializers/json.py", line 115, in default
    return super(DjangoJSONEncoder, self).default(o)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <Point object at 0x7f783f0c92e0> is not JSON serializable

推荐答案

默认情况下,Django序列化程序不提供ForeignKey值。因此您可以覆盖geojson序列化程序字段。例如:

from django.contrib.gis.serializers.geojson import Serializer 

class CustomSerializer(Serializer):

    def end_object(self, obj):
        for field in self.selected_fields:
            if field == 'pk':
                continue
            elif field in self._current.keys():
                continue
            else:
                try:
                    if '__' in field:
                        fields = field.split('__')
                        value = obj
                        for f in fields:
                            value = getattr(value, f)
                        if value != obj:
                            self._current[field] = value

                except AttributeError:
                    pass
        super(CustomSerializer, self).end_object(obj)

用法:

serializers = CustomSerializer()
ca = ClusterA.objects.all()
data = serializers.serialize(ca, geometry_field='location', fields=('activity__name', 'location', ))

这篇关于在Django GeoJSON序列化程序字段中指定相关型号的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在Django GeoJSON序列化程序字段中指定相关型号的字段

基础教程推荐