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

Dealing with JSONFields #94

Open
twang817 opened this issue Jun 30, 2022 · 0 comments
Open

Dealing with JSONFields #94

twang817 opened this issue Jun 30, 2022 · 0 comments

Comments

@twang817
Copy link

Given a JSONField in the model.

class MyModel(models.Model):
  data = models.JSONField()

And a custom lookup:

FooField(StrField):
  name = "foo"
  suggest_options = True
  def get_options(self, search):
    name = self.get_lookup_name()
    lookup = {}
    if search:
      lookup[f"{name}__icontains"] = search
    return self.model.objects.filter(**lookup).order_by(name).values_list(name, flat=True).distinct()
  def get_lookup_name(self):
    return "data__foo"

class MyModelQLSchema(DjangoQLSchema):
  def get_fields(self, model):
    fields = super().get_field(model)
    if model == MyModel:
      fields += [FooField(model=MyModel)]
    return fields

On Sqlite, I am able now to search for something like:

foo = "asdf"

But, on Postgres, I get an error:

Traceback (most recent call last):
  File "...", line 89, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedFunction: operator does not exist: json = unknown
LINE 1: ...("myapp_mymodel"."data" -> 'foo') = '"asdf"' ...

I am able to run the query:

select * from myapp_mymodel where (data->>'foo')::text = 'asdf'

This query also works (closer to the query that django is using):

select * from myapp_mymodel where (data->'foo')::text = '"asdf"'

The question is how do I implement a custom lookup that will make these casts? It appears that get_lookup wants Q objects. Is there a way I can work with annotations? Or directly manipulate the queryset?

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