Open alg opened 1 week ago
Hi! It's works for me
class UuidSerializableModelView(ModelView):
async def get_pk_value(
self,
request: Request,
obj: Any, # noqa: ANN401
) -> Any: # noqa: ANN401
pk = await super().get_pk_value(request, obj)
if isinstance(pk, UUID):
return str(pk)
return pk
class MyView(UuidSerializableModelView):
fields: Sequence[BaseField] = [
StringField("id", label="ID"),
]
@barbarrista I took a different approach, which I think is safer since It looks like get_pk_value
is supposed to return anything that is pk as is (not a string representation of UUID).
In my view subclass I override BaseView.serialize
like this:
@override
async def serialize(
self,
obj: Any,
request: Request,
action: RequestAction,
include_relationships: bool = True,
include_select2: bool = False,
) -> dict[str, Any]:
data = await super().serialize(obj, request, action, include_relationships, include_select2)
# Fixing non-serializable PK
if self.pk_attr and self.pk_attr in data:
pk_value = data[self.pk_attr]
if isinstance(pk_value, UUID):
data[self.pk_attr] = str(pk_value)
return data
It could be extracted in a class as you suggested. Just wondering if pk value should be passed through some method when forced in the original serialize
method so its serialization easier to configure and extend.
Both approaches are good workarounds but we can solve this by using the serialized pk value instead of the raw pk value in the serialize method. One way to achieve this is by adding another abstract method get_serialized_pk_value
in the BaseView
and then using self.pk_field.serialize_value
for sqla implementation.
Feel free to submit a PR.
@jowilf That's what I was thinking. I'm on it.
@alg great, thanks
Describe the bug When I have a model with the pk (id) field of UUID type and include it in the list, it works fine. But if I exclude it, while leaving among the fields, it gives an error:
To Reproduce
Environment (please complete the following information):