MongoEngine/mongoengine

How to modify the last item of a list field by positional operator?

cikay opened this issue · 0 comments

cikay commented

I need to update the last item of a list field positional operator something like below

class Tag(EmbeddedDocument):
    name = StringField(required=True)
    description = StringField()


class Blog(Document):
    tags = EmbeddedDocumentListField(Tag, required=True)
    content = StringField(required=True)

blog = Blog.objects.filter(id=id).first()
blog.modify(set__tags__0="Python")

In the code above I modify the first item of the tags field. It is not possible to use -1 in the keyword. So how to modify the last item of a list field by positional argument?

I know it is possible something like below

blog.tags[-1]  = "Python"
blog.save()

However, I implemented a custom signal and put it into the modify method for decoupling audit logs. If I use the save method for updates those updates will be missed in the audit logs.

What I need is something like this

blog.modify(**{"set__tags__-1": "Python"})