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.

How to call methods on Nested InnerDoc objects?

See original GitHub issue

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

github_iconTop GitHub Comments

1reaction
annawinklercommented, Dec 3, 2018

@HonzaKral I’m happy to contribute to the docs then 😃

0reactions
honzakralcommented, Dec 3, 2018

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 a list of objects is expected, if you expect to only have one address use Object instead and then you can skip the list in the example (just House(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!

Read more comments on GitHub >

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

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