Queryset calculated field

    0

    0

    Alec Meyer

    This code creates a Django Model that counts the number of tasks scheduled for a specific day. It then annotates this model so that it can be searched and filtered.

    from django.db.models import Count
    
    controls = ConMonControl.objects.annotate(
      task_count=Count('scheduled_tasks')
    ).annotate(
      has_tasks=models.Case(
        models.When(task_count__gt=0, then=True),
        default=False,
        output_field=models.BooleanField()
      )
    )
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.