2016-09-11 07:20:16 +08:00
|
|
|
from django.db import models
|
2015-04-05 00:10:26 +08:00
|
|
|
|
|
|
|
from .fields import (
|
2017-02-11 20:16:35 +08:00
|
|
|
ArrayField, BigIntegerRangeField, CICharField, CIEmailField, CITextField,
|
2019-02-04 07:27:19 +08:00
|
|
|
DateRangeField, DateTimeRangeField, DecimalRangeField, EnumField,
|
2019-06-09 08:56:37 +08:00
|
|
|
HStoreField, IntegerRangeField, SearchVectorField,
|
2015-01-11 00:14:20 +08:00
|
|
|
)
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Tag:
|
2015-07-23 01:51:05 +08:00
|
|
|
def __init__(self, tag_id):
|
|
|
|
self.tag_id = tag_id
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return isinstance(other, Tag) and self.tag_id == other.tag_id
|
|
|
|
|
|
|
|
|
|
|
|
class TagField(models.SmallIntegerField):
|
|
|
|
|
2017-07-07 01:18:05 +08:00
|
|
|
def from_db_value(self, value, expression, connection):
|
2015-07-23 01:51:05 +08:00
|
|
|
if value is None:
|
|
|
|
return value
|
|
|
|
return Tag(int(value))
|
|
|
|
|
|
|
|
def to_python(self, value):
|
|
|
|
if isinstance(value, Tag):
|
|
|
|
return value
|
|
|
|
if value is None:
|
|
|
|
return value
|
|
|
|
return Tag(int(value))
|
|
|
|
|
|
|
|
def get_prep_value(self, value):
|
|
|
|
return value.tag_id
|
|
|
|
|
|
|
|
|
2015-04-05 00:10:26 +08:00
|
|
|
class PostgreSQLModel(models.Model):
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
required_db_vendor = 'postgresql'
|
|
|
|
|
|
|
|
|
|
|
|
class IntegerArrayModel(PostgreSQLModel):
|
2017-08-18 07:21:35 +08:00
|
|
|
field = ArrayField(models.IntegerField(), default=list, blank=True)
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
|
2015-04-05 00:10:26 +08:00
|
|
|
class NullableIntegerArrayModel(PostgreSQLModel):
|
2014-03-27 00:44:21 +08:00
|
|
|
field = ArrayField(models.IntegerField(), blank=True, null=True)
|
2019-11-01 19:08:03 +08:00
|
|
|
field_nested = ArrayField(ArrayField(models.IntegerField(null=True)), null=True)
|
2020-12-03 16:30:33 +08:00
|
|
|
order = models.IntegerField(null=True)
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
|
2015-04-05 00:10:26 +08:00
|
|
|
class CharArrayModel(PostgreSQLModel):
|
2014-03-27 00:44:21 +08:00
|
|
|
field = ArrayField(models.CharField(max_length=10))
|
|
|
|
|
|
|
|
|
2015-04-05 00:10:26 +08:00
|
|
|
class DateTimeArrayModel(PostgreSQLModel):
|
2015-01-11 02:13:28 +08:00
|
|
|
datetimes = ArrayField(models.DateTimeField())
|
|
|
|
dates = ArrayField(models.DateField())
|
|
|
|
times = ArrayField(models.TimeField())
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
|
2015-04-05 00:10:26 +08:00
|
|
|
class NestedIntegerArrayModel(PostgreSQLModel):
|
2014-03-27 00:44:21 +08:00
|
|
|
field = ArrayField(ArrayField(models.IntegerField()))
|
2014-03-15 01:34:49 +08:00
|
|
|
|
|
|
|
|
2015-04-05 00:10:26 +08:00
|
|
|
class OtherTypesArrayModel(PostgreSQLModel):
|
2018-09-19 04:14:44 +08:00
|
|
|
ips = ArrayField(models.GenericIPAddressField(), default=list)
|
|
|
|
uuids = ArrayField(models.UUIDField(), default=list)
|
|
|
|
decimals = ArrayField(models.DecimalField(max_digits=5, decimal_places=2), default=list)
|
2015-07-23 01:51:05 +08:00
|
|
|
tags = ArrayField(TagField(), blank=True, null=True)
|
2019-06-09 08:56:37 +08:00
|
|
|
json = ArrayField(models.JSONField(default=dict), default=list)
|
2018-07-27 23:35:54 +08:00
|
|
|
int_ranges = ArrayField(IntegerRangeField(), blank=True, null=True)
|
|
|
|
bigint_ranges = ArrayField(BigIntegerRangeField(), blank=True, null=True)
|
2015-01-11 02:13:28 +08:00
|
|
|
|
|
|
|
|
2015-04-05 00:10:26 +08:00
|
|
|
class HStoreModel(PostgreSQLModel):
|
2014-03-15 01:34:49 +08:00
|
|
|
field = HStoreField(blank=True, null=True)
|
2017-05-03 12:24:54 +08:00
|
|
|
array_field = ArrayField(HStoreField(), null=True)
|
2014-09-06 04:53:11 +08:00
|
|
|
|
|
|
|
|
2019-02-04 07:27:19 +08:00
|
|
|
class ArrayEnumModel(PostgreSQLModel):
|
|
|
|
array_of_enums = ArrayField(EnumField(max_length=20))
|
|
|
|
|
|
|
|
|
2014-09-06 04:53:11 +08:00
|
|
|
class CharFieldModel(models.Model):
|
2021-09-15 18:57:49 +08:00
|
|
|
field = models.CharField(max_length=64)
|
2014-09-06 04:53:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TextFieldModel(models.Model):
|
|
|
|
field = models.TextField()
|
2014-12-21 18:21:37 +08:00
|
|
|
|
2015-06-01 05:45:03 +08:00
|
|
|
|
2019-12-05 16:54:27 +08:00
|
|
|
class SmallAutoFieldModel(models.Model):
|
|
|
|
id = models.SmallAutoField(primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class BigAutoFieldModel(models.Model):
|
|
|
|
id = models.BigAutoField(primary_key=True)
|
|
|
|
|
|
|
|
|
2015-06-01 05:45:03 +08:00
|
|
|
# Scene/Character/Line models are used to test full text search. They're
|
|
|
|
# populated with content from Monty Python and the Holy Grail.
|
|
|
|
class Scene(models.Model):
|
|
|
|
scene = models.CharField(max_length=255)
|
|
|
|
setting = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
|
|
|
|
class Character(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
|
|
|
|
|
2017-02-11 20:16:35 +08:00
|
|
|
class CITestModel(PostgreSQLModel):
|
|
|
|
name = CICharField(primary_key=True, max_length=255)
|
|
|
|
email = CIEmailField()
|
|
|
|
description = CITextField()
|
2017-05-03 13:25:30 +08:00
|
|
|
array_field = ArrayField(CITextField(), null=True)
|
2016-06-02 05:43:59 +08:00
|
|
|
|
|
|
|
|
2015-06-01 05:45:03 +08:00
|
|
|
class Line(PostgreSQLModel):
|
|
|
|
scene = models.ForeignKey('Scene', models.CASCADE)
|
|
|
|
character = models.ForeignKey('Character', models.CASCADE)
|
|
|
|
dialogue = models.TextField(blank=True, null=True)
|
|
|
|
dialogue_search_vector = SearchVectorField(blank=True, null=True)
|
|
|
|
dialogue_config = models.CharField(max_length=100, blank=True, null=True)
|
|
|
|
|
2014-12-21 18:21:37 +08:00
|
|
|
|
2020-03-04 20:33:12 +08:00
|
|
|
class LineSavedSearch(PostgreSQLModel):
|
|
|
|
line = models.ForeignKey('Line', models.CASCADE)
|
|
|
|
query = models.CharField(max_length=100)
|
|
|
|
|
|
|
|
|
2016-02-12 22:59:13 +08:00
|
|
|
class RangesModel(PostgreSQLModel):
|
|
|
|
ints = IntegerRangeField(blank=True, null=True)
|
|
|
|
bigints = BigIntegerRangeField(blank=True, null=True)
|
2018-10-03 07:17:23 +08:00
|
|
|
decimals = DecimalRangeField(blank=True, null=True)
|
2016-02-12 22:59:13 +08:00
|
|
|
timestamps = DateTimeRangeField(blank=True, null=True)
|
2019-07-10 16:33:36 +08:00
|
|
|
timestamps_inner = DateTimeRangeField(blank=True, null=True)
|
2016-02-12 22:59:13 +08:00
|
|
|
dates = DateRangeField(blank=True, null=True)
|
2019-07-10 16:33:36 +08:00
|
|
|
dates_inner = DateRangeField(blank=True, null=True)
|
2016-02-12 22:59:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RangeLookupsModel(PostgreSQLModel):
|
|
|
|
parent = models.ForeignKey(RangesModel, models.SET_NULL, blank=True, null=True)
|
|
|
|
integer = models.IntegerField(blank=True, null=True)
|
|
|
|
big_integer = models.BigIntegerField(blank=True, null=True)
|
|
|
|
float = models.FloatField(blank=True, null=True)
|
|
|
|
timestamp = models.DateTimeField(blank=True, null=True)
|
|
|
|
date = models.DateField(blank=True, null=True)
|
2019-12-05 16:54:27 +08:00
|
|
|
small_integer = models.SmallIntegerField(blank=True, null=True)
|
|
|
|
decimal_field = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
|
2014-12-21 18:21:37 +08:00
|
|
|
class ArrayFieldSubclass(ArrayField):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(models.IntegerField())
|
2015-02-08 23:21:48 +08:00
|
|
|
|
|
|
|
|
2020-10-12 18:52:58 +08:00
|
|
|
class AggregateTestModel(PostgreSQLModel):
|
2015-02-08 23:21:48 +08:00
|
|
|
"""
|
|
|
|
To test postgres-specific general aggregation functions
|
|
|
|
"""
|
|
|
|
char_field = models.CharField(max_length=30, blank=True)
|
|
|
|
integer_field = models.IntegerField(null=True)
|
2017-05-06 22:56:28 +08:00
|
|
|
boolean_field = models.BooleanField(null=True)
|
2020-10-12 18:52:58 +08:00
|
|
|
json_field = models.JSONField(null=True)
|
2015-02-08 23:21:48 +08:00
|
|
|
|
|
|
|
|
2020-10-12 18:52:58 +08:00
|
|
|
class StatTestModel(PostgreSQLModel):
|
2015-02-08 23:21:48 +08:00
|
|
|
"""
|
|
|
|
To test postgres-specific aggregation functions for statistics
|
|
|
|
"""
|
|
|
|
int1 = models.IntegerField()
|
|
|
|
int2 = models.IntegerField()
|
2015-07-22 22:43:21 +08:00
|
|
|
related_field = models.ForeignKey(AggregateTestModel, models.SET_NULL, null=True)
|
2015-06-02 01:00:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
class NowTestModel(models.Model):
|
|
|
|
when = models.DateTimeField(null=True, default=None)
|
2017-03-30 05:52:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
class UUIDTestModel(models.Model):
|
|
|
|
uuid = models.UUIDField(default=None, null=True)
|
2019-07-12 19:08:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Room(models.Model):
|
|
|
|
number = models.IntegerField(unique=True)
|
|
|
|
|
|
|
|
|
|
|
|
class HotelReservation(PostgreSQLModel):
|
|
|
|
room = models.ForeignKey('Room', on_delete=models.CASCADE)
|
|
|
|
datespan = DateRangeField()
|
|
|
|
start = models.DateTimeField()
|
|
|
|
end = models.DateTimeField()
|
|
|
|
cancelled = models.BooleanField(default=False)
|
2020-10-14 19:09:24 +08:00
|
|
|
requirements = models.JSONField(blank=True, null=True)
|