Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda