Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda