I have a problem with Django 1.5.4. I put the question in StackOverflow instead ofhttp://gis.stackexchange.com/ because I'm almost 100% sure is not a GIS related problem.
Here is my set up:
My 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)As you can see, Series model contains a reference to ROI model
My settings.py
SERIALIZATION_MODULES = {
'geojson': 'djgeojson.serializers'
}I'm using django-geojson to serialize my ROI objects into GeoJSON. I want to use this serializer to send a GeoJSON to my clients. So, my views.py looks like this
My 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')The problem: I'm getting this error in serialize call
AttributeError: 'ROI' object has no attribute 'roi'The relevant part of the error stack is this
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()]Looking at the stack, looks like there's a problem resolving the reverse reference of ROI to Series. Series has a roi field pointing to ROI, and I think the serializer thinks the roi field belongs to ROI class (incorrect) instead of to Series (correct). The expression infinite loop comes to my mind.
Besides, if I delete the roi field from Series model class, it works
I'm not sure about if it's a bug of django-geojson plugin (using the last version) or something wrong with my code (most likely). I've tried with the default json serializer, and still getting the same error. And also tried to inherit all model classes from gismodels instead of models. No effect.
Any clues?
Ok, I managed to solve it. Looks like there is a bug in the django-geojson plugin. It can't handle the backward relations. So, I just told Django to 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)