Search code examples
django

Django filtering by annotated field


I'm trying to sort only those objects that contain arrays in the JSON field

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

class JsonbTypeof(Func):
    function = 'jsonb_typeof'

Rule.objects.annotate(type=JsonbTypeof("data")).filter(type="array")
But this query always returns an empty queryset

However, if you call
Rule.objects.annotate(type=JsonbTypeof("data")).values("type")
then the expected queryset with a set of objects is returned:
<QuerySet [{'type': 'object'}, {'type': 'object'}, {'type': 'array'}, {'type': 'array'}]>

And if you do like this:
Rule.objects.annotate(type=Value("array")).filter(type="array")
Then the filtering is done correctly

Are there any experts who can tell me how to filter and leave only arrays?


Solution

  • You can specify the output field:

    from django.db.models import CharField
    
    
    class JsonbTypeof(Func):
        function = 'jsonb_typeof'
        output_field = CharField()

    such that Django knows it has to handle it as a string.