GeoDjango序列化程序和ForeignKey字段出错

时间:2014-04-06 19:02:06

标签: django python-2.7 serialization geojson

我遇到了 Django 1.5.4 的问题。我把问题放在StackOverflow而不是https://gis.stackexchange.com/,因为我几乎100%肯定不是与GIS相关的问题。

这是我的设置:

我的 models.py

from django.contrib.auth.models import User
from django.contrib.gis.db import models as gismodels

# This models a region of interest, using a polygon
class ROI(gismodels.Model):
    label = models.CharField(max_length=256, default='ROI')
    area = models.FloatField(default=0.0)
    geom = gismodels.PolygonField(srid=4326)
    when = models.DateTimeField(default=datetime.now())
    user = models.ForeignKey(User, null=True)

    objects = gismodels.GeoManager()

    def __unicode__(self):
        return unicode(self.label)

    class Meta:
        ordering = ['when']


class Indicator(models.Model):
    name = models.TextField()
    color = models.TextField()
    measurement_units = models.CharField(max_length=100)
    algorithm = models.CharField(max_length=256)
    data_origin = models.TextField()


class Series(models.Model):
    roi = models.ForeignKey(ROI)
    indicator = models.ForeignKey(Indicator)

如您所见,系列模型包含对ROI模型的引用

我的 settings.py

SERIALIZATION_MODULES = {
    'geojson': 'djgeojson.serializers'
}

我正在使用django-geojson 将我的投资回报率对象序列化为GeoJSON 。我想使用此序列化程序将GeoJSON发送给我的客户端。所以,我的views.py看起来像这样

我的 views.py

@login_required
def get_rois(request):
    rois_query = ROI.objects.filter(user=request.user)

    polygons = json.loads(serializers.serialize('geojson', rois_query))

    return HttpResponse(json.dumps(polygons), mimetype='application/json')

问题:我在序列化调用中遇到此错误

AttributeError: 'ROI' object has no attribute 'roi'

错误堆栈的相关部分就是这个

File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/django/core/serializers/__init__.py", line 122, in serialize
    s.serialize(queryset, **options)
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/djgeojson/serializers.py", line 349, in serialize
    self.serialize_queryset(queryset)
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/djgeojson/serializers.py", line 321, in serialize_queryset
    self.handle_reverse_field(obj, field, field_name)
  File "/home/vagrant/.virtualenvs/myapp/local/lib/python2.7/site-packages/djgeojson/serializers.py", line 243, in handle_reverse_field
    values = [reverse_value(related) for related in getattr(obj, field_name).iterator()]

查看堆栈,看起来解决ROISeries反向引用时出现问题。 Series有一个roi字段指向ROI,我认为序列化程序认为roi字段属于ROI类(不正确),而不是Series 1}}(正确)。我想到了表达式无限循环

此外,如果我从roi模型类中删除Series字段,则可以

我不确定这是否是django-geojson插件(使用最新版本)或我的代码(最有可能)的错误。我已经尝试使用默认的json序列化程序,但仍然得到相同的错误。并且还试图从gismodels而不是模型继承所有模型类。没效果。

任何线索?

1 个答案:

答案 0 :(得分:0)

好的,我设法解决了。看起来像there is a bug in the django-geojson plugin。它无法处理落后关系。所以,我刚刚告诉Django avoid the backward relation creation between ROI and Series

class Series(gismodels.Model):
    # Prevents django to create a backwards relation between ROI and Series
    roi = models.ForeignKey(ROI, related_name='+')
    indicator = models.ForeignKey(Indicator)