Skip to content

Instantly share code, notes, and snippets.

@ageron
Created April 8, 2013 17:15
Show Gist options
  • Save ageron/5338551 to your computer and use it in GitHub Desktop.
Save ageron/5338551 to your computer and use it in GitHub Desktop.
>>> from mongoengine import *
>>> class FacebookUser(Document):
... meta = { "allow_inheritance": False }
... facebook_id = StringField(primary_key = True, max_length = 20)
... name = StringField()
... def __unicode__(self):
... return u"/".join((self.facebook_id, self.name or u""))
...
>>> connect("test")
Connection('localhost', 27017)
>>> FacebookUser.objects.delete()
>>> FacebookUser.objects
[]
>>> FacebookUser.objects.get_or_create(facebook_id = "1234")
(<FacebookUser: 1234/>, True)
>>> FacebookUser.objects
[]
>>> FacebookUser.objects.get_or_create(facebook_id = "1234", name = "John Smith")
(<FacebookUser: 1234/John Smith>, True)
>>> FacebookUser.objects
[<FacebookUser: 1234/John Smith>]
>>> FacebookUser.objects.get_or_create(facebook_id = "5678", name = "David Peters")
(<FacebookUser: 5678/David Peters>, True)
>>> FacebookUser.objects
[<FacebookUser: 1234/John Smith>, <FacebookUser: 5678/David Peters>]
>>> FacebookUser.objects.get_or_create(facebook_id = "1234", name = "Bjarne Stroustrup")
(<FacebookUser: 1234/Bjarne Stroustrup>, True)
>>> FacebookUser.objects
[<FacebookUser: 5678/David Peters>, <FacebookUser: 1234/Bjarne Stroustrup>]
@ageron
Copy link
Author

ageron commented Apr 8, 2013

Line 15 says that the object was created when it fact it wasn't (as line 17 shows). Isn't that a bug?

@ageron
Copy link
Author

ageron commented Apr 8, 2013

When I add extra attributes, such as the name (line 18), then the object is properly saved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment