On Sunday, April 6, 2014 11:13:30 PM UTC+2, Jorge Arevalo wrote:
>
> (Sorry for the cross posting. I'm not sure about the best place to put my 
> question. So, I put it in StackOverflow too. And I know that a post that 
> contains just a link to another page can be considered spam. So, I C&P the 
> content here)
>
> 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 Userfrom django.contrib.gis.db import 
> models as gismodels
> # This models a region of interest, using a polygonclass 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 <https://github.com/makinacorpus/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_requireddef 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 <https://github.com/makinacorpus/django-geojson/issues/23>. It can't 
handle the backward relations. So, I just told Django to *avoid the 
backward relation creation* between ROI and 
Series<https://docs.djangoproject.com/en/1.5/ref/models/fields/#django.db.models.ForeignKey.related_name>

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)


 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/482c97aa-3827-4efa-80b9-9fa1359821d0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to