Amsterdam / django-gisserver

Django speaking WFS 2.0 to expose geo data
Mozilla Public License 2.0
38 stars 10 forks source link

Exposing complex fields #9

Closed robertpoziumschi closed 3 years ago

robertpoziumschi commented 3 years ago

Hi guys,

Is there any way to expose fields like ImageField (the url) or ManyToManyField (list of ids)?

I tried the following for ImageField but it didn't work:

Thank you, Robert

vdboor commented 3 years ago

Hi @robertpoziumschi

It seems you've touched a unpolished spot there. We're currently not using value_from_object(), only getattr(model_instance, fieldname). I'm not that familiar with value_from_object(), should that be the preferred route?

Fortunately, the current field structure does allow you to override attribute retrieval.

from gisserver.features import FeatureField
from gisserver.types import XsdElement

class XsdImageElement(XsdElement):
    def get_value(self, instance):
        return getattr(instance, self.model_attribute)  # mimics current behavior
        return self.source.object_from_object(instance)  # would call your function

class FeatureImageField(FeatureField):
    xsd_element_class = XsdImageElement

Then you can use the FeatureImageField in the FeatureType definition directly.

feature = FeatureType(
    fields=[
       "id",
       "name",
       FeatureImageField("image"),
    ]
)

Seeing the complexity, I do think we should provide some shortcuts to simplify this (e.g. allow overriding xsd_element_classdirectly from thefield()` function)

vdboor commented 3 years ago

I've added 2 fixes in the master branch:

So the example above becomes:

class XsdImageElement(XsdElement):
    def get_value(self, instance):
        return self.source.object_from_object(instance)

feature = FeatureType(
    fields=[
       "id",
       "name",
       field("image", xsd_class=XsdImageElement),
    ]
)
robertpoziumschi commented 3 years ago

Thank you very much!

vdboor commented 2 years ago

@robertpoziumschi FYI, version 1.2 has just been released with M2M field support too.