question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fields and `to_dict`

See original GitHub issue

I’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:closed
  • Created 8 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
honzakralcommented, Feb 9, 2016

CustomField added in a5b614d2ab7c6a2fdd49e9d01a0204bd2fd0c565, now only to add documentation…

1reaction
honzakralcommented, Feb 9, 2016

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 the to_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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found