Allow querying JSONField with F objects #29769

182 views
Skip to first unread message

Mani S

unread,
Oct 27, 2018, 3:58:54 PM10/27/18
to Django developers (Contributions to Django itself)
Django's F objects does not perform JSON lookups. A ticket has been raised for the same https://code.djangoproject.com/ticket/29769

I have written a customer expression for querying JSONField and it works!

class KeyTextTransformFactory:

    def __init__(self, key_name):
        self.key_name = key_name

    def __call__(self, *args, **kwargs):
        return KeyTextTransform(self.key_name, *args, **kwargs)

class JSONF(F):

    def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
        rhs = super().resolve_expression(query, allow_joins, reuse, summarize, for_save)

        field_list = self.name.split(LOOKUP_SEP)
        for name in field_list[1:]:
            rhs = KeyTextTransformFactory(name)(rhs)
        return rhs

It is necessary to include Cast in rhs,

Sample.objects.filter(jsonfield__lookup__value=Cast(JSONF('value'), IntegerField())) 




Note: This is my first contribution in Django source code, any comments/suggestions would help me learn the process better.





Thank you,
Mani

Mani S

unread,
Nov 10, 2018, 9:51:57 PM11/10/18
to django-d...@googlegroups.com
Hi devs,
Any update on this? Would like to know your thoughts




Thanks,
Mani

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/88f52ca0-ebd9-4139-b88e-9f21292b7603%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Josh Smeaton

unread,
Nov 13, 2018, 6:05:33 AM11/13/18
to Django developers (Contributions to Django itself)
Hi Mani,

No response can mean either the readers here do not have the same problem and have no comment, or your implementation does not require much discussion. In any case, you could continue with the process by proposing a formal patch, and getting someone to review it once it meets the requirements for a finished patch.


The change should come with tests and documentation.

Regarding your implementation, I'm not thrilled with the requirement for users to provide a Cast. If casting needs to occur, it'd be preferable to have the interface of JSONF(lookup, output_field), and then wrap self with Cast(self, self.output_field).

Is there any particular reason why you chose a separate expression type rather than containing the functionality within the existing F expression? I'd be keen to hear the pros/cons of each.
Reply all
Reply to author
Forward
0 new messages