Django model with embedded documents issues error: coercing to Unicode: need string or buffer, xxx found
-
21-12-2019 - |
Question
I am trying to define models in Django with a list of embedded documents using mongoengine to connect to my mongodb data. Here is the relevant part of my models.py:
class Feature(EmbeddedDocument):
title = StringField(max_length=100, required=True)
is_property = BooleanField(default=True)
property_type = StringField(max_length=50, choices=PROPERTY_TYPE)
def __unicode__(self):
return self.title or u''
class Property(EmbeddedDocument):
feature = EmbeddedDocumentField(Feature)
value = StringField(max_length=2000, required=True)
def __unicode__(self):
return self.feature
class Concept(Document):
title = StringField(max_length=200, required=True)
properties = ListField(EmbeddedDocumentField(Property))
In console, when I try to create a new Concept, i do the following:
f = Feature(title='start time',property_type='Time')
p = Property(feature=f, value='6:30pm')
c = Concept(title='test', properties = [p])
c.save() #or c.properties[0]
I then get the following error:
c.properties
Out[36]: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2731, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-36-47a09d2ccd39>", line 1, in <module>
c.properties
File "/usr/lib/python2.7/dist-packages/IPython/core/displayhook.py", line 238, in __call__
format_dict = self.compute_format_data(result)
File "/usr/lib/python2.7/dist-packages/IPython/core/displayhook.py", line 150, in compute_format_data
return self.shell.display_formatter.format(result)
File "/usr/lib/python2.7/dist-packages/IPython/core/formatters.py", line 126, in format
data = formatter(obj)
File "/usr/lib/python2.7/dist-packages/IPython/core/formatters.py", line 447, in __call__
printer.pretty(obj)
File "/usr/lib/python2.7/dist-packages/IPython/lib/pretty.py", line 345, in pretty
return self.type_pprinters[cls](obj, self, cycle)
File "/usr/lib/python2.7/dist-packages/IPython/lib/pretty.py", line 529, in inner
p.pretty(x)
File "/usr/lib/python2.7/dist-packages/IPython/lib/pretty.py", line 360, in pretty
return _default_pprint(obj, self, cycle)
File "/usr/lib/python2.7/dist-packages/IPython/lib/pretty.py", line 480, in _default_pprint
p.text(repr(obj))
File "/usr/local/lib/python2.7/dist-packages/mongoengine-0.8.6-py2.7.egg/mongoengine/base/document.py", line 201, in __repr__
u = self.__str__()
File "/usr/local/lib/python2.7/dist-packages/mongoengine-0.8.6-py2.7.egg/mongoengine/base/document.py", line 212, in __str__
return unicode(self).encode('utf-8')
TypeError: coercing to Unicode: need string or buffer, Feature found
Solution
I think you need to explicitly convert the Feature
to unicode
, because mongoengine calls .encode
on the __unicode__
of your object:
return unicode(self).encode('utf-8')
Where self
is an instance of Property
, and unicode(self)
would return your Feature
instance. So something like this should work
class Property(...):
# ...
def __unicode__(self):
return unicode(self.feature)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow