Closed robertpoziumschi closed 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 the
field()` function)
I've added 2 fixes in the master branch:
FeatureField
too: 915da46dbd35009d825e6a29a1b7b3ec91a6888cvalue_from_object()
: 4e5084a1a79c681d3468745e95388babccbc04a0So 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),
]
)
Thank you very much!
@robertpoziumschi FYI, version 1.2 has just been released with M2M field support too.
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