{"action":"created","issue":{"url":"https://api.github.com/repos/elastic/elasticsearch-dsl-py/issues/160","labels_url":"https://api.github.com/repos/elastic/elasticsearch-dsl-py/issues/160/labels{/name}","comments_url":"https://api.github.com/repos/elastic/elasticsearch-dsl-py/issues/160/comments","events_url":"https://api.github.com/repos/elastic/elasticsearch-dsl-py/issues/160/events","html_url":"https://github.com/elastic/elasticsearch-dsl-py/issues/160","id":75996662,"number":160,"title":"Persisting Nested Objects","user":{"login":"andybarilla","id":5983808,"avatar_url":"https://avatars.githubusercontent.com/u/5983808?v=3","gravatar_id":"","url":"https://api.github.com/users/andybarilla","html_url":"https://github.com/andybarilla","followers_url":"https://api.github.com/users/andybarilla/followers","following_url":"https://api.github.com/users/andybarilla/following{/other_user}","gists_url":"https://api.github.com/users/andybarilla/gists{/gist_id}","starred_url":"https://api.github.com/users/andybarilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/andybarilla/subscriptions","organizations_url":"https://api.github.com/users/andybarilla/orgs","repos_url":"https://api.github.com/users/andybarilla/repos","events_url":"https://api.github.com/users/andybarilla/events{/privacy}","received_events_url":"https://api.github.com/users/andybarilla/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2015-05-13T14:10:55Z","updated_at":"2015-05-13T14:49:19Z","closed_at":null,"body":"Note: Cross-posting from http://discuss.elastic.co. Not sure where is better for elasticsearch-dsl questions.\r\n\r\nI'm attempting to persist an object to elasticsearch with a nested object. However instead of using syntax like: \r\n\r\n    child = field.Object(properties={'name': field.String()})\r\n\r\nI'd like to have child be it's own object. \r\n\r\n    from elasticsearch_dsl import field, document\r\n    from elasticsearch_dsl import connections\r\n    connections.connections.create_connection(hosts=['localhost'], timeout=20)\r\n    \r\n    \r\n    class Child(field.Object):\r\n        name = field.String()\r\n    \r\n    \r\n    class Parent(document.DocType):\r\n        name = field.String()\r\n        child = Child()\r\n    \r\n        class Meta:\r\n            index = 'myindex'\r\n    \r\n    parent = Parent(name='test parent')\r\n    parent.child = Child(name='test other child')\r\n    parent.save()\r\n\r\nHowever, doing it this way results in:\r\n\r\n      File \"/home/abarilla/PycharmProjects/iocdb/iocdb/model/test.py\", line 25, in <module>\r\n        parent.child = Child(name='test other child')\r\n      File \"/home/abarilla/.virtualenvs/iocdb/local/lib/python2.7/site-packages/elasticsearch_dsl/document.py\", line 115, in __setattr__\r\n        return super(DocType, self).__setattr__(name, value)\r\n      File \"/home/abarilla/.virtualenvs/iocdb/local/lib/python2.7/site-packages/elasticsearch_dsl/utils.py\", line 418, in __setattr__\r\n        value = self._doc_type.mapping[name].to_python(value)\r\n      File \"/home/abarilla/.virtualenvs/iocdb/local/lib/python2.7/site-packages/elasticsearch_dsl/field.py\", line 63, in to_python\r\n        return self._to_python(data)\r\n      File \"/home/abarilla/.virtualenvs/iocdb/local/lib/python2.7/site-packages/elasticsearch_dsl/field.py\", line 133, in _to_python\r\n        return self._doc_class(self.properties, **data)\r\n    TypeError: type object argument after ** must be a mapping, not Child\r\n\r\nIs there a way I can accomplish this?"},"comment":{"url":"https://api.github.com/repos/elastic/elasticsearch-dsl-py/issues/comments/101697710","html_url":"https://github.com/elastic/elasticsearch-dsl-py/issues/160#issuecomment-101697710","issue_url":"https://api.github.com/repos/elastic/elasticsearch-dsl-py/issues/160","id":101697710,"user":{"login":"HonzaKral","id":32132,"avatar_url":"https://avatars.githubusercontent.com/u/32132?v=3","gravatar_id":"","url":"https://api.github.com/users/HonzaKral","html_url":"https://github.com/HonzaKral","followers_url":"https://api.github.com/users/HonzaKral/followers","following_url":"https://api.github.com/users/HonzaKral/following{/other_user}","gists_url":"https://api.github.com/users/HonzaKral/gists{/gist_id}","starred_url":"https://api.github.com/users/HonzaKral/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HonzaKral/subscriptions","organizations_url":"https://api.github.com/users/HonzaKral/orgs","repos_url":"https://api.github.com/users/HonzaKral/repos","events_url":"https://api.github.com/users/HonzaKral/events{/privacy}","received_events_url":"https://api.github.com/users/HonzaKral/received_events","type":"User","site_admin":false},"created_at":"2015-05-13T14:49:19Z","updated_at":"2015-05-13T14:49:19Z","body":"There are two different things at play here - the definition of the Nested mappings, which can currently be done, although not through the declarative syntax:\r\n\r\n    user_field = Nested(\r\n        properties={\r\n          \"display_name\": String(fields={'raw': String(index='not_analyzed')})\r\n        }\r\n    )\r\n\r\n    class BlogPost(DocType):\r\n        author = user_fielc\r\n\r\nIf I understand correctly you'd also want to wrap all instances of `author` into your own python class. To do that you have to create a custom subclass and speify the `_doc_class` property (you should use a subclass of `InnerObjectWrapper`:\r\n\r\n    class Author(Nested):\r\n        _doc_class = MyAuthorClass\r\n\r\n   \r\n    user_field = Author(\r\n        properties={\r\n          \"display_name\": String(fields={'raw': String(index='not_analyzed')})\r\n        }\r\n    )\r\n\r\n    class BlogPost(DocType):\r\n        author = user_fielc\r\n\r\nI am certainly open to making this easier, do you have any ideas how you'd like the API to look?\r\n\r\nThanks!\r\n\r\n\r\n"}}