fields.Method result should be able to be wrapped into other field types
See original GitHub issuefields.Method is pretty much always used to return a certain type of value. It doesn’t allow to choose options for the return value though.
For example, if one designes a field which should return a generic datetime and format it using a given format string, one is forced to use fields.Method which is linked to a schema method which returns a string(!), not a datetime. Because it is now impossible to provide a format string for the datetime anywhere except of the method itself.
In other words, something like this:
class MySchema(Schema):
created_at = fields.Datetime(method='_created_at', format='%Y-%m-%dT%H:%M:%S%z')
def _created_at(self, obj):
return obj.metadata().creation_time()
would be much more convenient and reusable than this:
class MySchema(Schema):
created_at = fields.Method('_formatted_created_at')
def _formatted_created_at(self, obj):
return obj.metadata().created_at().strftime('%Y-%m-%dT%H:%M:%S%z')
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Customizing the behavior of cached fields - Apollo GraphQL
If a field requires numerous parameters then each parameter must be wrapped in a variable that is then destructured and returned. Each parameter...
Read more >Fields - C# Programming Guide | Microsoft Learn
A field in C# is a variable of any type that is declared directly in a class or struct. Fields are members of...
Read more >Fields — marshmallow 3.19.0 documentation
Concrete Field classes should implement this method. Parameters: value – The value to be serialized. attr (str) – The attribute or key on...
Read more >Factory function for dataclass fields - python - Stack Overflow
I was thinking I could write a factory function to wrap dataclass.field and help reduce the amount of repetition. However, I can't seem...
Read more >Fields Method - TechDocs - Broadcom
Gen™ 8.6. Version 8.6. 8.6 8.5. Open/Close Topics Navigation. Product Menu. Topics. Announcement and News · Release Notes.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
from_field
already exists. It is theattribute
parameter.We could add parameters
dump_method
andload_method
. And deprecateMethod
field.And likewise
*_function
, unless there is a way to do both functions and methods with the same parameter.Any progress on this?