Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cikay opened this issue May 30, 2023 · 0 comments
Open

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

cikay opened this issue May 30, 2023 · 0 comments

Comments

@cikay
Copy link

cikay commented May 30, 2023

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"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant