Adding and updating ListField with Mongoengine -
using mongoengine , trying form tag cloud. each item attach 1 or more tags. similar tags used here (below each question asked).
after searching , reading many posts here, still can't proper way of adding new entries listfield, or how replace them.
class item(document): tags = listfield(stringfield(max_length=300))
i'm trying push 1 or more new tags, using form , gather posted results. in views.py have following check:
if 'tags' in request.post , request.post['tags'] <> '': tag in request.post.getlist('tags'): itemdata.update(push__tags__s__tags=tag)
when trying push it, fails:
validationerror (profile:5185505b73ea128e878f4e82) (only lists , tuples may used in list field: ['tags'])
obviously i'm using wrong type, i'm lost on how 1 solved. strange thing reason data appended record though.. (posted "test" , refreshed browser)
"tags" : [ "test", "test" ] }
can 1 show me small example how deal posted string (from html form) , push listfield (and how replace them all).
thanks!
you don't need positional operator $
equates __s__
in mongoengine aren't replacing / updating position in list.
as don't want repeat tags, should use $addtoset. can in mongoengine so:
itemdata.update(add_to_set__tags=['tag1', 'tag2'])
passing in list add_to_set
automatically convert $addtoset
$each
.
Comments
Post a Comment