From e3c432aa379723ff7c55f65698960bc9ee74d00d Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 15 Jan 2025 08:21:15 -0600 Subject: [PATCH] fix: add `serialize_object()` method for `ObjectRef` schema node so we can use this node type for non-wutta mapped class, with non-uuid primary key --- src/wuttaweb/forms/schema.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/wuttaweb/forms/schema.py b/src/wuttaweb/forms/schema.py index b3f0105..77e443e 100644 --- a/src/wuttaweb/forms/schema.py +++ b/src/wuttaweb/forms/schema.py @@ -334,8 +334,21 @@ class ObjectRef(colander.SchemaType): # nb. keep a ref to this for later use node.model_instance = appstruct - # serialize to uuid - return appstruct.uuid.hex + # serialize to PK as string + return self.serialize_object(appstruct) + + def serialize_object(self, obj): + """ + Serialize the given object to its primary key as string. + + Default logic assumes the object has a UUID; subclass can + override as needed. + + :param obj: Object reference for the node. + + :returns: Object primary key as string. + """ + return obj.uuid.hex def deserialize(self, node, cstruct): """ """ @@ -417,7 +430,7 @@ class ObjectRef(colander.SchemaType): if 'values' not in kwargs: query = self.get_query() objects = query.all() - values = [(obj.uuid.hex, str(obj)) + values = [(self.serialize_object(obj), str(obj)) for obj in objects] if self.empty_option: values.insert(0, self.empty_option)