summaryrefslogtreecommitdiff
path: root/tests/lookup/models.py
blob: 9bb3412c33ebbe5b5b453c78bbc492032a39d61c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
The lookup API

This demonstrates features of the database API.
"""

from django.db import models
from django.db.models.lookups import IsNull


class Alarm(models.Model):
    desc = models.CharField(max_length=100)
    time = models.TimeField()

    def __str__(self):
        return "%s (%s)" % (self.time, self.desc)


class Author(models.Model):
    name = models.CharField(max_length=100)
    alias = models.CharField(max_length=50, null=True, blank=True)

    class Meta:
        ordering = ("name",)


class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateTimeField()
    author = models.ForeignKey(Author, models.SET_NULL, blank=True, null=True)
    slug = models.SlugField(unique=True, blank=True, null=True)

    class Meta:
        ordering = ("-pub_date", "headline")

    def __str__(self):
        return self.headline


class Tag(models.Model):
    articles = models.ManyToManyField(Article)
    name = models.CharField(max_length=100)

    class Meta:
        ordering = ("name",)


class NulledTextField(models.TextField):
    def get_prep_value(self, value):
        return None if value == "" else value


class NullField(models.Field):
    pass


NullField.register_lookup(IsNull)


@NulledTextField.register_lookup
class NulledTransform(models.Transform):
    lookup_name = "nulled"
    template = "NULL"

    @property
    def output_field(self):
        return NullField()


@NulledTextField.register_lookup
class IsNullWithNoneAsRHS(IsNull):
    lookup_name = "isnull_none_rhs"
    can_use_none_as_rhs = True


class Season(models.Model):
    year = models.PositiveSmallIntegerField()
    gt = models.IntegerField(null=True, blank=True)
    nulled_text_field = NulledTextField(null=True)

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=["year"], name="season_year_unique"),
        ]

    def __str__(self):
        return str(self.year)


class Game(models.Model):
    season = models.ForeignKey(Season, models.CASCADE, related_name="games")
    home = models.CharField(max_length=100)
    away = models.CharField(max_length=100)


class Player(models.Model):
    name = models.CharField(max_length=100)
    games = models.ManyToManyField(Game, related_name="players")


class Product(models.Model):
    name = models.CharField(max_length=80)
    qty_target = models.DecimalField(max_digits=6, decimal_places=2)


class Stock(models.Model):
    product = models.ForeignKey(Product, models.CASCADE)
    short = models.BooleanField(default=False)
    qty_available = models.DecimalField(max_digits=6, decimal_places=2)


class Freebie(models.Model):
    gift_product = models.ForeignKey(Product, models.CASCADE)
    stock_id = models.IntegerField(blank=True, null=True)

    stock = models.ForeignObject(
        Stock,
        from_fields=["stock_id", "gift_product"],
        to_fields=["id", "product"],
        on_delete=models.CASCADE,
    )