kristian
kristian

Reputation: 760

Why can't I assign the values of a ListField directly?

I am having troubles creating a new document with mongoengine (python3). It seems not possible to directly add lists to ListFields.

I have the following set up:

# CONNECTION AND SETUP:

from mongoengine import *

connect('mongoengine_testing', host='localhost', port=27017)

class Chart(Document):
    instrument_ticker = StringField(max_length=40)
    chart_type = StringField(max_length=120)
    chart_name = StringField(max_length=120)
    x = ListField(StringField)
    y = ListField(StringField)

When I try to add a new Chart document like this it fails:

## THIS DOESN'T WORK:

chart = Chart(
        instrument_ticker = 'EURUSD',
        chart_type = 'weekday_avg',
        chart_name = 'Average Weekday',
        x = ['1', '2', '3', '4', '5'],
        y = ['13', '12', '24', '55', '32']
)


### ERROR MESSAGE

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-17d67eea1df7> in <module>()
      4         chart_name = 'Average Weekday',
      5         x = ['1', '2', '3', '4', '5'],
----> 6         y = ['13', '12', '24', '55', '32']
      7 )

~/Development/python/mongoengine/mongo_env/lib/python3.4/site-packages/mongoengine/base/document.py in __init__(self, *args, **values)
    113                         field = self._fields.get(key)
    114                         if field and not isinstance(field, FileField):
--> 115                             value = field.to_python(value)
    116                     setattr(self, key, value)
    117                 else:

~/Development/python/mongoengine/mongo_env/lib/python3.4/site-packages/mongoengine/base/fields.py in to_python(self, value)
    324             self.field._auto_dereference = self._auto_dereference
    325             value_dict = {key: self.field.to_python(item)
--> 326                           for key, item in list(value.items())}
    327         else:
    328             Document = _import_class('Document')

~/Development/python/mongoengine/mongo_env/lib/python3.4/site-packages/mongoengine/base/fields.py in <dictcomp>(.0)
    324             self.field._auto_dereference = self._auto_dereference
    325             value_dict = {key: self.field.to_python(item)
--> 326                           for key, item in list(value.items())}
    327         else:
    328             Document = _import_class('Document')

TypeError: to_python() missing 1 required positional argument: 'value'

But when I create it in multiple steps like this it works:

## THIS WORKS:

chart = Chart(
        instrument_ticker = 'EURUSD',
        chart_type = 'weekday_avg',
        chart_name = 'Average Weekday',
)

chart.x = ['1', '2', '3', '4', '5']
chart.y = ['13', '12', '24', '55', '32']

Is this expected behavior? Or what am I missing?

Upvotes: 3

Views: 1210

Answers (1)

kristian
kristian

Reputation: 760

The problem was that I forgot the brackets after StringField.. ouch.

class Chart(Document):
    ...
    x = ListField(StringField())
    y = ListField(StringField())

Upvotes: 11

Related Questions