MongoEngine / MongoEngine/mongoengine
How to modify the last item of a list field by positional operator?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
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"})
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing how the supplied blog.modify call converts set__tags__0 into an update and how MongoDB handles positional list paths. Compare that path with the existing blog.tags[-1] and blog.save() behavior; done means the last tag can be changed through modify while the existing audit signal still records the update.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- database
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100