Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Evaluating boolean expressions and annotating the result

Let's say I have a model:

class Measurement(models.Model):
    measurements = JSONField(
        null=True, blank=True, help_text="Key value pairs for measurements."
    )

    timestamp = models.DateTimeField(auto_now_add=True)

    class Meta:
        get_latest_by = "timestamp"

The values within the measurements are VARIABLE amounts of key value pairs with boolean values but the values could also be null. For example:

{
    "a": true,
    "b": false, // optional
    "c": null // optional
    //etc...can be infinite amount of values.
}

I need to be able to AND those together. For example:

result_expression = F("measurements__a") & F("measurements__b") & F("measurements__c") & F("measurement__...")

Obviously, this doesn't work.

Also, how would I be able to annotate the results of that? Measurement.objects.annotate(result=result_expression).values("result")

Where the result here should be False because of the null and False values.

How would I go about doing this? I'm aware that I can do this purely with Python. I'd rather not do that. If there's a Postgres or Django way to do it, I'd rather do that.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can check if the three values are True with:

Measurement.objects.annotate(
    measurements__a=True,
    measurements__b=True,
    measurements__c=True
).values('result')

or you can annotate with a Case expression [Django-doc]:

from django.db.models import BooleanField, Case, Value, When

Measurement.objects.annotate(
    result=Case(
        When(
            measurements__a=True,
            measurements__b=True,
            measurements__c=True,
            value=Value(True)
        ),
        default=Value(False),
        output_field=BooleanField()
    )
).values('result')

For a variable number of keys, we can use dictionary unpacking:

from django.db.models import BooleanField, Case, Value, When

items = ['a', 'b', 'c']

Measurement.objects.annotate(
    result=Case(
        When(
            **{f'measurements__{k}': True for k in items },
            value=Value(True)
        ),
        default=Value(False),
        output_field=BooleanField()
    )
).values('result')
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!