Adding and updating ListField with Mongoengine
Asked Answered
H

1

8

Using Mongoengine and trying to form a tag cloud. To each item I would like to attach one or more tags. Something similar like the tags are used here (below each question asked).

After searching and reading many posts here, I still can't the proper way of adding new entries to a ListField, or how to replace them.

class Item(Document):
    tags = ListField(StringField(max_length=300))

I'm trying to push one or more new tags, by using a form and gather the posted results. In my views.py I have the following check:

if 'tags' in request.POST and request.POST['tags'] <> '':
   for Tag in request.POST.getlist('tags'):
       ItemData.update(push__tags__S__tags=Tag)

When trying to push it, it fails:

ValidationError (Profile:5185505b73ea128e878f4e82) (Only lists and tuples may be used in a list field: ['tags'])

Obviously I'm using the wrong type, but I'm lost on how to get this one solved. Strange thing is that for some reason the data is appended to the record though.. (posted "test" and refreshed browser)

"tags" : [ "test", "test" ] }

Can one show me a small example how to deal with a posted string (from a HTML form) and push it properly into a ListField (and how to replace them all).

Herman answered 19/5, 2013 at 21:41 Comment(0)
M
16

You don't need the positional operator $ which equates to __S__ in mongoengine as you aren't replacing / updating a position in the list.

As you probably don't want to repeat the tags, you should use $addToSet. You can do this in mongoengine like so:

ItemData.update(add_to_set__tags=['tag1', 'tag2'])

Passing in a list to add_to_set will automatically convert it to an $addToSet with $each.

Mcleroy answered 20/5, 2013 at 7:57 Comment(5)
Thanks Ross. It still leaves me with the ValidationError though (only lists and tuples). When receiving the data from a form post, how can I push the right format into it?Herman
Convert the post data to be a list or a tuple.Mcleroy
Even your example gives me the validation error. Using x=request.POST.getlist('tags'), which should give a list as well, same error. After googling for hours and trying to split etc, I really can't see the cause. In the meantime I upgraded from 0.8.0RC1 to 0.8.0RC4, but doesn't solve. Any extra points would be really appreciated! Love Mongoengine, but the learning curve is holding me back a bit :)Herman
Found the reason after reading your example. My list was originally empty, so it refused saving it (even though to my understanding I was providing it a list to append). When I save a single tag first, then append with the add_to_set, it works fine. Thanks for the pointer!Herman
@Herman it's many years on, but nevertheless I will write it anyway for someone else who stumbles across it you had tags = ListField(StringField(max_length=300)) add default=[] as it will negate the checking for the array and you'll be adding to it as intended.Uella

© 2022 - 2024 — McMap. All rights reserved.