Skip to content

Instantly share code, notes, and snippets.

@cmheisel
Created June 17, 2012 15:47
Show Gist options
  • Save cmheisel/2944918 to your computer and use it in GitHub Desktop.
Save cmheisel/2944918 to your computer and use it in GitHub Desktop.
mongoengine: Attribute error when using db_field and setting a property
# Blank to start
from mongoengine import connect, Document, StringField
connect('gist-2944918')
class Kard(Document):
"""
Represents a card on a Kanban board.
"""
key = StringField(required=True, unique=True)
title = StringField()
_service_class = StringField(required=False, db_field="service_class")
@property
def service_class(self):
return self._service_class or "Default"
Kard.objects.all().delete()
k = Kard(key="IPHONE-1", title="Build an iPhone", _service_class="Epic")
k.save()
k2 = Kard(key="IPHONE-2", title="Build an iPhone 2")
k2.save()
no_class = Kard.objects.get(key="IPHONE-2") # Works
epic = Kard.objects.get(key="IPHONE-1") # Attribute error
"""
Traceback (most recent call last):
File "dbfield_bug.py", line 28, in <module>
epic = Kard.objects.get(key="IPHONE-1")
File "/Users/chris/Dropbox/repos/gist-2944918/.fe/lib/python2.7/site-packages/mongoengine/queryset.py", line 777, in get
result1 = self.next()
File "/Users/chris/Dropbox/repos/gist-2944918/.fe/lib/python2.7/site-packages/mongoengine/queryset.py", line 957, in next
return self._document._from_son(self._cursor.next())
File "/Users/chris/Dropbox/repos/gist-2944918/.fe/lib/python2.7/site-packages/mongoengine/base.py", line 980, in _from_son
obj = cls(**data)
File "/Users/chris/Dropbox/repos/gist-2944918/.fe/lib/python2.7/site-packages/mongoengine/base.py", line 801, in __init__
setattr(self, key, value)
File "/Users/chris/Dropbox/repos/gist-2944918/.fe/lib/python2.7/site-packages/mongoengine/base.py", line 846, in __setattr__
super(BaseDocument, self).__setattr__(name, value)
AttributeError: can't set attribute
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment