You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a list of nested objects which have a property that can act as a sort key. However, that property is not part of the schema since it's not useful in the dumped version.
I'm looking for a way to sort the data using this property.
pre_dump won't work because i'd need to modify the actual object to be dumped - not a good idea in case of ORM model instances
post_dump won't work because i don't have the data
post_dump(pass_original=True) would be cumbersome because i'd have to get the order from the original ones but then apply it to the dumped data
The ideal option would be a way to intercept how the data for the List() gets loaded. That way I could simply return a sorted list without touching the original one. Is there already a way to do that? Something like @get_value('mylistattribute') to override how it gets loaded from the data-to-be-dumped? The only other alternative I see is subclassing List and add sorting support in there...
The text was updated successfully, but these errors were encountered:
FWIW this is what I ended up using, but if there's a better way please let me know:
classSortedList(fields.List):
""" Like the normal List, but when dumping a sort key can be specified. This allows sorting the data even without having the information needed for sorting in the final dumped data. """def__init__(self, *args, sort_key, **kwargs):
self.sort_key=sort_keysuper().__init__(*args, **kwargs)
def_serialize(self, value, attr, obj, **kwargs):
ifvalueisNone:
returnNonevalue=sorted(value, key=self.sort_key)
returnsuper()._serialize(value, attr, obj, **kwargs)
I have a list of nested objects which have a property that can act as a sort key. However, that property is not part of the schema since it's not useful in the dumped version.
I'm looking for a way to sort the data using this property.
pre_dump
won't work because i'd need to modify the actual object to be dumped - not a good idea in case of ORM model instancespost_dump
won't work because i don't have the datapost_dump(pass_original=True)
would be cumbersome because i'd have to get the order from the original ones but then apply it to the dumped dataThe ideal option would be a way to intercept how the data for the
List()
gets loaded. That way I could simply return a sorted list without touching the original one. Is there already a way to do that? Something like@get_value('mylistattribute')
to override how it gets loaded from the data-to-be-dumped? The only other alternative I see is subclassingList
and add sorting support in there...The text was updated successfully, but these errors were encountered: