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

__contains does not support parenthesis #665

Open
LakshmiPrabhaN opened this issue Apr 19, 2023 · 3 comments
Open

__contains does not support parenthesis #665

LakshmiPrabhaN opened this issue Apr 19, 2023 · 3 comments

Comments

@LakshmiPrabhaN
Copy link

LakshmiPrabhaN commented Apr 19, 2023

One line description of the issue
__contains in filter does not support parenthesis and gives empty queryset as a result

Example, i need to get this object
{
"channel": '["Admin (role)"]'
}

Python script

models.py

`from djongo import models
import jsonfield

class word(models.Model):
channel = jsonfield.JSONField()`

views.py

queryset = word.objects.filter(channel__contains="Admin (role)")

It translates the query as the following
filter: { channel: { '$regex': '^.*"Admin (role)".*$' } }

But we need the translated query
filter: { channel: { '$regex': '^.*"Admin \\(role\\)".*$' } }

Also tried word.objects.filter(channel__contains="Admin \\(role\\)") but it is not working

Env

Django==2.2.24
django-jsonfield==1.4.1
djangorestframework==3.11.0
djongo==1.3.6
dnspython==2.3.0
pymongo==3.12.1
pytz==2022.7.1
six==1.16.0
sqlparse==0.2.4

@pimuzzo
Copy link

pimuzzo commented May 16, 2023

the same thing happens to me with the "+" character

@IntelligentSynthesis
Copy link

IntelligentSynthesis commented Jun 2, 2023

Code
self.to_match = re.sub(r'([]()[{}.*+^$?|\/])', r'\\\g<1>', self.to_match)

Only add one line to operators.py.
image

Code
self.to_match = re.escape(self.to_match)
I also found a solution in #620. This is a more simple way!
If you are using the older version (not master) of 1.3.6, you can modify operators.py as #620.

@Assassin9520
Copy link

OffTopic:

t translates the query as the following
filter: { channel: { '$regex': '^."Admin (role)".$' } }

How can i see the translated query from ORM to mongoDB? looking for too much time now :(

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

4 participants