Debian Codegraph @

tests/lookup/models.py

raw
1 """
2 The lookup API
3
4 This demonstrates features of the database API.
5 """
6
7 from django.db import models
8 from django.db.models.lookups import IsNull
9
10
11 class Alarm(models.Model):
12 desc = models.CharField(max_length=100)
13 time = models.TimeField()
14
15 def __str__(self):
16 return "%s (%s)" % (self.time, self.desc)
17
18
19 class Author(models.Model):
20 name = models.CharField(max_length=100)
21 alias = models.CharField(max_length=50, null=True, blank=True)
22 bio = models.TextField(null=True)
23
24 class Meta:
25 ordering = ("name",)
26
27
28 class Article(models.Model):
29 headline = models.CharField(max_length=100)
30 pub_date = models.DateTimeField()
31 author = models.ForeignKey(Author, models.SET_NULL, blank=True, null=True)
32 slug = models.SlugField(unique=True, blank=True, null=True)
33
34 class Meta:
35 ordering = ("-pub_date", "headline")
36
37 def __str__(self):
38 return self.headline
39
40
41 class Tag(models.Model):
42 articles = models.ManyToManyField(Article)
43 name = models.CharField(max_length=100)
44
45 class Meta:
46 ordering = ("name",)
47
48
49 class NulledTextField(models.TextField):
50 def get_prep_value(self, value):
51 return None if value == "" else value
52
53
54 @NulledTextField.register_lookup
55 class NulledTransform(models.Transform):
56 lookup_name = "nulled"
57 template = "NULL"
58
59
60 @NulledTextField.register_lookup
61 class IsNullWithNoneAsRHS(IsNull):
62 lookup_name = "isnull_none_rhs"
63 can_use_none_as_rhs = True
64
65
66 class Season(models.Model):
67 year = models.PositiveSmallIntegerField()
68 gt = models.IntegerField(null=True, blank=True)
69 nulled_text_field = NulledTextField(null=True)
70
71 class Meta:
72 constraints = [
73 models.UniqueConstraint(fields=["year"], name="season_year_unique"),
74 ]
75
76 def __str__(self):
77 return str(self.year)
78
79
80 class Game(models.Model):
81 season = models.ForeignKey(Season, models.CASCADE, related_name="games")
82 home = models.CharField(max_length=100)
83 away = models.CharField(max_length=100)
84
85
86 class Player(models.Model):
87 name = models.CharField(max_length=100)
88 games = models.ManyToManyField(Game, related_name="players")
89
90
91 class Product(models.Model):
92 name = models.CharField(max_length=80)
93 qty_target = models.DecimalField(max_digits=6, decimal_places=2)
94
95
96 class Stock(models.Model):
97 product = models.ForeignKey(Product, models.CASCADE)
98 short = models.BooleanField(default=False)
99 qty_available = models.DecimalField(max_digits=6, decimal_places=2)
100
101
102 class Freebie(models.Model):
103 gift_product = models.ForeignKey(Product, models.CASCADE)
104 stock_id = models.IntegerField(blank=True, null=True)
105
106 stock = models.ForeignObject(
107 Stock,
108 from_fields=["stock_id", "gift_product"],
109 to_fields=["id", "product"],
110 on_delete=models.CASCADE,
111 )