How to call methods on Nested InnerDoc objects?
See original GitHub issueI have a Document class with Nested objects, and I’d like to call validation methods on those Nested objects. What’s the best way to do that? Here is a simple example that shows what I am trying to do:
class AddressParts(InnerDoc):
house_number = Integer(required=True)
street_name = Keyword(required=True)
city_name = Keyword(required=True)
def validate(self):
if house_number < 0:
raise RuntimeError('Ooops this isn't a valid house number!')
class AddressData(InnerDoc):
address = Nested(AddressParts, required=True)
def validate(self):
address.validate()
class House(Document):
address_data = Nested(AddressData, required=True)
def validate(self):
address_data.validate()
Because the fields are dicts, I then tried converting them to their objects like this:
def validate(self):
AddressData(address_data.to_dict()).validate()
I get an error though AttributeError: 'list' object has no attribute 'to_dict'
… so I’m curious what the best approach is for what I’m trying to do?
Thanks! Anna
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Javascript call methods from nested objects in loop
You need to access the inner object using the key. Also, return name as a property, not a method call. var obj =...
Read more >Nested field type | Elasticsearch Guide [8.5] | Elastic
The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way...
Read more >Nested Classes - Learning the Java Language
As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to...
Read more >Persistence — Elasticsearch DSL 7.2.0 documentation
In case of Object or Nested fields an instance of the InnerDoc subclass should ... object or create the mappings directly by calling...
Read more >Nested Classes in Java - GeeksforGeeks
i.e., an object of a static nested class is not strongly associated with the outer class object.As with class methods and variables, ...
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
@HonzaKral I’m happy to contribute to the docs then 😃
I created a PR in your repo which shows the API how it works in https://github.com/annawinkler/es_address_book/pull/1
Also note that
Nested
is meant to be used only when alist
of objects is expected, if you expect to only have oneaddress
useObject
instead and then you can skip the list in the example (justHouse(address=AddressData(...))
)I also implemented a
clean
method which is a built-in (undocumented, my bad!) hook for users to add their own validation logic like you have done.Hope this helps!