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

Unable to filter nested fields in JSONField using Django ORM #682

Open
eibrahimov opened this issue Sep 27, 2023 · 3 comments
Open

Unable to filter nested fields in JSONField using Django ORM #682

eibrahimov opened this issue Sep 27, 2023 · 3 comments

Comments

@eibrahimov
Copy link

I am using Djongo to integrate MongoDB with my Django project. I have a model with a JSONField and I am trying to filter based on a nested field within the JSONField. Here's a simplified version of my model and the query I am attempting:

from djongo import models as mongo_models

class ProcessIDs(mongo_models.Model):
    data = mongo_models.JSONField(max_length=1000, default=dict())
    # ... other fields ...

# Example object creation
process = ProcessIDs.objects.create(
    data={'redirect_ids': {'ids': ['1234567', '7654321'], 'status': 'active'}, "category_type": "custom"}
)

# Attempted query
active_processes = ProcessIDs.objects.filter(data__redirect_ids__status__exact='active')

This query results in the following error:
FieldError: Unsupported lookup ‘redirect_ids’ for JSONField or join on the field not permitted.

I am looking for a way to filter by the nested field data->redirect_ids->status using Django ORM with Djongo. Is this a known limitation, or is there a workaround to achieve this?

@raykhey
Copy link

raykhey commented Oct 9, 2023

Try active_processes = ProcessIDs.objects.filter(data={"redirect_ids":{"status":'active'}})

@panrobot
Copy link

panrobot commented Dec 27, 2023

Hi,

[EDIT]
I have done this with a djongo version before Nov 13 merge. Maybe now it is fixed but still not uploaded to PIP so try with building from repo and use syntax proposed by @raykhey
[/EDIT]

the problem is with LikeOp and CmpOp from operators.py - there should be a recursive dictionary crawler to build a proper Mongo query. My workaround was:

  1. clone repository
  2. fix LikeOp by adding some method to crawl dictionary and CmpOp to properly build query
  3. build a pip package using local repo (e.g. SO)
  4. use syntax proposed by @raykhey

@Uysim
Copy link

Uysim commented Apr 30, 2024

Did any one find the solution for this? Actually, this is the basic need for normal query.

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