Search code examples
pymongomongoengine

Does Mongoengine allow pull from <variable>?


I have a few pull queries that look like this:

Site.objects(siteid).update_one(pull__somelist__refid=myid)  

I would like to reuse this code by making 'somelist' a variable - something like this:

listvar = 'somelist'
Site.objects(siteid).update_one(pull__<listvar>__refid=myid)  

I have tried various wrappers such as [listvar] and (listvar) without success.

Is there a way to inject the variable value into the query?


Solution

  • You should be able to abuse the kwarg notation for this

    myvar = "some_var"
    funky_kwarg = {f"pull__{myvar}__refid": myid}
    Site.objects(siteid).update_one(**funky_kwarg)