Ticket #35830: django_bug.py

File django_bug.py, 3.1 KB (added by Paul Landon Tuckett, 5 weeks ago)
Line 
1from django.db import models
2from django.utils import timezone
3
4from project.enums.core import TargetType
5
6__all__ = [
7 "Target",
8]
9
10class Target(models.Model):
11 """
12 Model storing team/user metric targets.
13
14 """
15
16 id = models.IntegerField(primary_key=True)
17 created_at = models.DateTimeField(default=timezone.now)
18 updated_at = models.DateTimeField(default=timezone.now)
19
20 key = models.TextField()
21 org = models.ForeignKey("flmodels.Organization", related_name="targets", on_delete=models.DO_NOTHING)
22 team = models.ForeignKey("flmodels.Team", null=True, on_delete=models.DO_NOTHING)
23 apex_user = models.ForeignKey("flmodels.ApexUser", null=True, on_delete=models.DO_NOTHING)
24
25 value = models.FloatField()
26 target_type_id = models.IntegerField(choices=TargetType.choices)
27
28
29follow_targets = Target.objects.filter(key="follow:max")
30# output:
31# <QuerySet [<Target id=39180 key='follow:max' org=org_id name=redacted slug=redacted team=team_id name=redacted apex_user=None>,
32# <Target id=39310 key='follow:max' org=org_id name=redacted team=team_id name=redacted apex_user=None>,
33# <Target id=39291 key='follow:max' org=org_id name=redacted slug=redacted team=None apex_user=None>,
34# <Target id=39543 key='follow:max' org=org_id name=redacted team=None apex_user=None>,
35# <Target id=37798 key='follow:max' org=org_id name=redacted slug=redacted> team=team_id name=redacted> apex_user=None>,
36# '...(remaining elements truncated)...']>
37
38org_targets = Target.objects.filter(org_id=8006)
39# output:
40# <QuerySet [<Target id=39180 key='follow:max' org=<Organization id=8006 name=redacted slug=redacted team=team_id name=redacted apex_user=None>,
41# <Target id=39345 key='reaction:tgt' org=<Organization id=8006 name=redacted team=team_id name=redacted apex_user=None>,
42# <Target id=39346 key='reaction:tgt' org=<Organization id=8006 name=redacted slug=redacted team=redacted name=redacted apex_user=None>,
43# <Target id=40341 key='reaction:tgt' org=<Organization id=8006 name=redacted slug=redacted team=team_id name=redacted apex_user=None>,
44# '...(remaining elements truncated)...']>
45
46
47union_targets = follow_targets.union(org_targets)
48print(union_targets.query)
49# (
50# SELECT
51# "core_targets"."id" AS "col1",
52# "core_targets"."created_at" AS "col2",
53# "core_targets"."updated_at" AS "col3",
54# "core_targets"."key" AS "col4",
55# "core_targets"."org_id" AS "col5",
56# "core_targets"."team_id" AS "col6",
57# "core_targets"."apex_user_id" AS "col7",
58# "core_targets"."value" AS "col8",
59# "core_targets"."target_type_id" AS "col9"
60# FROM
61# "core_targets"
62# WHERE "core_targets"."key" = follow:max
63# ) UNION (
64# SELECT
65# "core_targets"."id" AS "col1",
66# "core_targets"."created_at" AS "col2",
67# "core_targets"."updated_at" AS "col3",
68# "core_targets"."key" AS "col4",
69# "core_targets"."org_id" AS "col5",
70# "core_targets"."team_id" AS "col6",
71# "core_targets"."apex_user_id" AS "col7",
72# "core_targets"."value" AS "col8",
73# "core_targets"."target_type_id" AS "col9"
74# FROM
75# "core_targets"
76# WHERE "core_targets"."org_id" = 8006
77# )
78
79
Back to Top