# Error when indexing using elasticsearch\_dsl library in python

**URL:** <https://discuss.elastic.co/t/error-when-indexing-using-elasticsearch-dsl-library-in-python/131212>\
**Category:** Elasticsearch\
**Created:** [May 9, 2018, 5:44pm UTC](https://discuss.elastic.co/t/error-when-indexing-using-elasticsearch-dsl-library-in-python/131212 "2018-05-09T17:44:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ammar\_Khan](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Ammar\_Khan](https://discuss.elastic.co/u/Ammar_Khan)\
**Post date:** [May 9, 2018, 5:44pm UTC](https://discuss.elastic.co/t/error-when-indexing-using-elasticsearch-dsl-library-in-python/131212/1 "2018-05-09T17:44:20Z")

</div>

Hello everyone!

I am working in django, a python framework. I am having an error while I am indexing my data. The error is just related when I insert Nested type when defining a doctype.

I have the following models:

class PostUser(models.Model):

```
user_id = models.CharField(max_length=1000,blank=True,null=True)
reputation = models.CharField(max_length = 1000 , blank = True , null = True)

def __unicode__ (self):
    return self.user_id

def indexing(self):
    obj = PostUserIndex(
        meta = {'id': self.id},
        user_id = self.user_id,
        reputation = self.reputation,
    )
    obj.save(index = 'post-user-index')
    return obj.to_dict(include_meta=True)

```

class Posts(models.Model):

```
user_post_id = models.CharField(max_length = 1000 , blank = True , null = True)
score = models.CharField(max_length = 1000 , blank = True , null = True)
owner_user_id = models.ForeignKey(PostUser,default="-100")

def __unicode__ (self):

    return self.user_post_id

def indexing(self):
    obj = PostsIndex(
        meta = {'id': self.id},
        user_post_id = self.user_post_id,
        score = self.score,
        owner_user_id = self.owner_user_id,
    )
    obj.save(index = 'newuserposts-index')
    return obj.to_dict(include_meta=True)

```

I am trying to index as follow:

class PostUserIndex(DocType):  
user\_id = Text()  
reputation = Text()

class PostsIndex(DocType):  
user\_post\_id = Text()  
score = Text()  
owner\_user\_id = Nested(PostUserIndex)

Then i run the following method to index data:

def posts\_indexing():  
PostsIndex.init(index='newuserposts-index')  
es = Elasticsearch()  
bulk(client=es, actions=(b.indexing() for b in models.Posts.objects.all().iterator()))

It throws the following error and the full stack is as below:

Traceback (most recent call last):  
File "", line 1, in   
File "/Users/ammarkhan/Desktop/danny/src/dataquerying/datatoindex.py", line 74, in new\_user\_posts\_indexing  
bulk(client=es, actions=(b.indexing() for b in models.Posts.objects.all().iterator()))  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch/helpers/ **init**.py", line 257, in bulk  
for ok, item in streaming\_bulk(client, actions, \*\*kwargs):  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch/helpers/ **init**.py", line 180, in streaming\_bulk  
client.transport.serializer):  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch/helpers/ **init**.py", line 58, in \_chunk\_actions  
for action, data in actions:  
File "/Users/ammarkhan/Desktop/danny/src/dataquerying/datatoindex.py", line 74, in   
bulk(client=es, actions=(b.indexing() for b in models.Posts.objects.all().iterator()))  
File "/Users/ammarkhan/Desktop/danny/src/dataquerying/models.py", line 167, in indexing  
obj.save(index = 'newuserposts-index')  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch\_dsl/document.py", line 405, in save  
self.full\_clean()  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch\_dsl/utils.py", line 417, in full\_clean  
self.clean\_fields()  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch\_dsl/utils.py", line 403, in clean\_fields  
data = field.clean(data)  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch\_dsl/field.py", line 179, in clean  
data = super(Object, self).clean(data)  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch\_dsl/field.py", line 90, in clean  
data = self.deserialize(data)  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch\_dsl/field.py", line 86, in deserialize  
return self.\_deserialize(data)  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch\_dsl/field.py", line 166, in \_deserialize  
return self.\_wrap(data)  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch\_dsl/field.py", line 142, in \_wrap  
return self.\_doc\_class.from\_es(data)  
File "/Users/ammarkhan/Desktop/danny/lib/python2.7/site-packages/elasticsearch\_dsl/utils.py", line 342, in from\_es  
meta = hit.copy()  
AttributeError: ‘PostUser' object has no attribute 'copy'

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 6, 2018, 5:44pm UTC](https://discuss.elastic.co/t/error-when-indexing-using-elasticsearch-dsl-library-in-python/131212/2 "2018-06-06T17:44:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
