Fields and `to_dict`
See original GitHub issueI’m currently trying to implement a new Field
to abstract the storage of a complex object. The goal is to continue using this object in the python code, and have it stored correctly in Elasticsearch:
class ComplexField(Field):
def to_dict(self):
d = super().to_dict()
return {
# my representation
}
def to_python(self, data):
return Complex(*data)
class MyDoctype(DocType):
complex = ComplexField()
complex = Complex()
md = MyDoctype()
md.complex = complex
The problem is that I never see to_dict called when the object is saved, but to_python is called during the initialization.
How can I solve this ?
thank you.
Issue Analytics
- State:
- Created 8 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Python dictionary from an object's fields - Stack Overflow
I thought I'd take some time to show you how you can translate an object to dict via dict(obj) . class A(object): d...
Read more >Dictionary Key and Fields | ClickHouse Docs
The structure clause describes the dictionary key and fields available for queries.
Read more >Get a dictionary from an Objects Fields - GeeksforGeeks
In this article, we will discuss how to get a dictionary from object's field i.e. how to get the class members in the...
Read more >Message and Field - Proto Plus for Python - Read the Docs
classmethod to_dict (instance, *, use_integers_for_enums=True, ... Messages and map fields are represented as dicts, repeated fields are represented as ...
Read more >dataclasses — Data Classes — Python 3.11.1 documentation
The dataclass() decorator examines the class to find field s. ... This method compares the class as if it were a tuple of...
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
CustomField
added in a5b614d2ab7c6a2fdd49e9d01a0204bd2fd0c565, now only to add documentation…Just pushed to master commits f404c8b and 4d2bfd9
I decided to go with the name (de)serialize for the methods, there is an example in the tests for a custom field (https://github.com/elastic/elasticsearch-dsl-py/blob/master/test_elasticsearch_dsl/test_document.py#L42-L59) showing how it would work. I still intend to create a
CustomField
helper to make it easier (avoid having the user reimplement theto_dict
method).I am not 100% sure on the naming, but the functionality seems to work just fine. I would be happy for any feedback.
Thanks