makinacorpus / django-geojson

django-geojson is a collection of helpers to (de)serialize (Geo)Django objects into GeoJSON.
GNU Lesser General Public License v3.0
258 stars 70 forks source link

Properties defined on Models are not showing up via GeoJSONLayerView #79

Closed askpatrickw closed 5 years ago

askpatrickw commented 8 years ago

I have a @property defined on my Model that works fine in django shell, but when I attempt to use it with GeoJSONLayerView it does not come through in the data.

models.py

 @property
    def popupContent(self):
        return '<p>Name: {}</p><p>County: {}</p>'.format(
          self.name,
          self.county)

Works fine in Shell

>>> from wa.models import Water
>>> Water.objects.all()[0].popupContent
'<p>Name: Aberdeen Lake</p><p>County: Grays Harbor</p>'

In urls.py I have specified the property

 url(r'^data/$', GeoJSONLayerView.as_view(
        model=Water, 
        properties=('popupContent')
        ), name='data'),

but in my data its not coming through, only the geometry is. If I specify a field like 'name' in properties then those field values work.

image

Maybe this Is this a regression of #31

Thanks

XaviTorello commented 5 years ago

@askpatrickw probably you have been solved it, but if it can be useful for someone, it should work passing the list of properties to be rendered, I mean:

 url(r'^data/$', GeoJSONLayerView.as_view(
        model=Water, 
        properties=['popupContent', 'anotherField', ...]
        ), name='data'),
askpatrickw commented 5 years ago

Hi @XaviTorello, I see in your example you use "anotherField" as an additional example. My issue is with Methods that have the @property decorator, not a model field.

Have you tested this with a model method decorated with @property? As in the docs: https://docs.djangoproject.com/en/2.1/topics/db/models/#model-method

XaviTorello commented 5 years ago

Yes, I've it working in some projects. Did you have the chance to test it with a recent version?

Something like:


urls.py

urlpatterns = [
    ...
    url(r'^data/location$',
        GeoJSONLayerView.as_view(
            model=models.Location,
            properties=['name', 'html']
        ),
        name='location-data',
    ),
    ...
]

models.py

class Location(models.Model):
    ...

    @property
    def html(self):
        return '<h1>{}</h1><p><{}</p>'.format(
            self.name,
            self.description
        )

will return:

{
    "type":"FeatureCollection",
    "features":[
        {
            "type":"Feature",
            "properties":{
                "name":"Coll d'Ordino",
                "html":"<h1>Coll d'Ordino</h1><p><Lorem ipsum...</p>",
                "model":"api.location"
            },
            "id":1,
            "geometry":{
                "type":"Point",
                "coordinates":[
                    1.5722207,
                    42.5549682
                ]
            }
        }
....
  ]
}
askpatrickw commented 5 years ago

I'm not actively using this any longer so I'm going to close this as I won't be able to assist and the original issue was reported 3 years ago.