Advanced Django query

    0

    3

    This code prints the following results:

    id name due_date status controls_list frequency created last_updated due_date 1 ScheduledTask,LeapMotion 1/1/2015 Ok Overdue 1/1/2015 1/1/2015 2 ScheduledTask,LeapMotion 1/2/2015 Ok Overdue 1/2/2015 1/2/2015 3 ScheduledTask,LeapMotion 1/3/2015 Ok Overdue 1/3/2015 1/3/2015

    tasks = list(
      ScheduledTask.objects.filter(system=system, completed=False)
      .filter(Q(controls__compliance_level=system.compliance_level) | Q(controls__isnull=True))
      .annotate(
        status=Case(
          When(due_date__lt=dt.now(), then=Value("Overdue")),
          default=Value("Ok"),
          output_field=CharField(max_length=10),
        )
      )
      .annotate(controls_list=StringAgg("controls__control_id", ", "))
      .values(
        "id", "name", "due_date", "status", "controls_list", "frequency", "created", "last_updated", "due_date"
      )
    )
    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.