Advertisement
SparrowG

Embedded Document

Jun 9th, 2011
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.35 KB | None | 0 0
  1. ==model.py==
  2. class ContentRating(EmbeddedDocument):
  3.     content = ReferenceField(Content)
  4.     rating = FloatField(default=5000)
  5.    
  6.  
  7.        
  8. class Target(Document):
  9.     age = IntField(min_value=20,max_value=60)
  10.     gender = StringField(max_length=1,choices=['M','F'])
  11.     location = GeoPointField()
  12.     videos = ListField(EmbeddedDocumentField(ContentRating))
  13.  
  14. ==django shell==
  15. >>>cr1=ContentRating()
  16. >>>cr1.content=Content.objects(title="Video3")
  17. >>>
  18. >>>for x in range(20,30):
  19. ...    t=Target()
  20. ...    t.age=x
  21. ...    t.gender='M'
  22. ...    t.location=[10,20]
  23. ...    t.videos.append(cr1)
  24. ...    t.save()
  25.  
  26. Traceback (most recent call last):
  27.   File "<console>", line 7, in <module>
  28.   File "/usr/local/lib/python2.6/dist-packages/mongoengine-0.4-py2.6.egg/mongoengine/document.py", line 73, in save
  29.     self.validate()
  30.   File "/usr/local/lib/python2.6/dist-packages/mongoengine-0.4-py2.6.egg/mongoengine/base.py", line 357, in validate
  31.     field._validate(value)
  32.   File "/usr/local/lib/python2.6/dist-packages/mongoengine-0.4-py2.6.egg/mongoengine/base.py", line 103, in _validate
  33.     self.validate(value)
  34.   File "/usr/local/lib/python2.6/dist-packages/mongoengine-0.4-py2.6.egg/mongoengine/fields.py", line 346, in validate
  35.     raise ValidationError('Invalid ListField item (%s)' % str(item))
  36. ValidationError: Invalid ListField item (ContentRating object)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement