Mongoengine: Document Queryset: TypeError: __init__() got an unexpected keyword argument '__auto_convert'

Created on 20 May 2018  路  7Comments  路  Source: MongoEngine/mongoengine

Using mongoengine=0.15.0, pymongo=3.6.1.

When calling a Document.objects, I get a Queryset for the Collection, but when trying to iterate through it, creating the actual Document from the set fails with:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sdarryanto/Documents/workspace/betterblend/venv/lib/python3.6/site-packages/mongoengine/queryset/queryset.py", line 99, in _iter_results
    self._populate_cache()
  File "/Users/sdarryanto/Documents/workspace/betterblend/venv/lib/python3.6/site-packages/mongoengine/queryset/queryset.py", line 118, in _populate_cache
    self._result_cache.append(next(self))
  File "/Users/sdarryanto/Documents/workspace/betterblend/venv/lib/python3.6/site-packages/mongoengine/queryset/base.py", line 1473, in __next__
    only_fields=self.only_fields)
  File "/Users/sdarryanto/Documents/workspace/betterblend/venv/lib/python3.6/site-packages/mongoengine/base/document.py", line 721, in _from_son
    obj = cls(__auto_convert=False, _created=created, __only_fields=only_fields, **data)
TypeError: __init__() got an unexpected keyword argument '__auto_convert'

Any ideas?

Looking at the base.document.Document class, __auto_convert attribute definitely exists.

Most helpful comment

still I couldn't reproduce error with your code. Also, document init method must be like this, that maybe the problem

def __init__(self, *args, **values):

All 7 comments

Thought it was because I was using [email protected] but went down to [email protected] and still having the same issue.

can you provide the code to reproduce this error?

It happens with any of my Documents. Here's an example:

class Tokens(Document):
    def __init__(self, kwargs):
        super(Tokens, self).__init__(**kwargs)
        try:
            self.access_token = kwargs['access_token']
            self.refresh_token = kwargs['refresh_token']
            self.scope = kwargs['scope']
            self.token_type = kwargs['token_type']

            self.curr_time = datetime.now()
            self.expires_in = kwargs['expires_in']

            self.expiration = self.curr_time + timedelta(seconds=self.expires_in)
            self.id = kwargs['_id']
        except:
            pass

    meta = {
        'collection': 'tokens',
    }

    access_token = StringField()
    expiration = DateTimeField()
    expires_in = IntField()
    refresh_token = StringField()
    scope = StringField()
    token_type = StringField()
    id = StringField(primary_key=True)

When jumping into a python shell, I'll do:

Tokens.objects

I'll get the stack trace posted above.

What's weird is that when I do a Tokens.objects.count(), I get a response.

@erdenezul

still I couldn't reproduce error with your code. Also, document init method must be like this, that maybe the problem

def __init__(self, *args, **values):

@erdenezul is right, the error comes from the line

class Tokens(Document):
    def __init__(self, kwargs):    # It is missing the "**" from **kwargs

Looking at the stack trace it fails when it loads the object from the database into the Tokens constructor. I could reproduce with the following:

import mongoengine as me
me.connect()

class Tokens(me.Document):
    access_token = me.StringField()

    def __init__(self, kwargs):
        super(Tokens, self).__init__(**kwargs)

Tokens._get_collection().insert_one({'access_token': 'blablablah'})
Tokens.objects()    # raises TypeError: __init__() got an unexpected keyword argument '__auto_convert'

I believe this can be closed

Was this page helpful?
0 / 5 - 0 ratings