2015-04-05 00:10:26 +08:00
|
|
|
from django.db import connection, models
|
|
|
|
|
|
|
|
from .fields import (
|
2015-01-28 20:35:27 +08:00
|
|
|
ArrayField, BigIntegerRangeField, DateRangeField, DateTimeRangeField,
|
2015-05-31 05:13:58 +08:00
|
|
|
FloatRangeField, HStoreField, IntegerRangeField, JSONField,
|
2015-01-11 00:14:20 +08:00
|
|
|
)
|
2014-03-27 00:44:21 +08:00
|
|
|
|
|
|
|
|
2015-04-05 00:10:26 +08:00
|
|
|
class PostgreSQLModel(models.Model):
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
required_db_vendor = 'postgresql'
|
|
|
|
|
|
|
|
|
|
|
|
class IntegerArrayModel(PostgreSQLModel):
|
2014-03-27 00:44:21 +08:00
|
|
|
field = ArrayField(models.IntegerField())
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
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):
|
2015-01-11 02:13:28 +08:00
|
|
|
ips = ArrayField(models.GenericIPAddressField())
|
|
|
|
uuids = ArrayField(models.UUIDField())
|
|
|
|
decimals = ArrayField(models.DecimalField(max_digits=5, decimal_places=2))
|
|
|
|
|
|
|
|
|
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)
|
2014-09-06 04:53:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CharFieldModel(models.Model):
|
|
|
|
field = models.CharField(max_length=16)
|
|
|
|
|
|
|
|
|
|
|
|
class TextFieldModel(models.Model):
|
|
|
|
field = models.TextField()
|
2014-12-21 18:21:37 +08:00
|
|
|
|
|
|
|
|
2016-02-12 22:59:13 +08:00
|
|
|
class RangesModel(PostgreSQLModel):
|
|
|
|
ints = IntegerRangeField(blank=True, null=True)
|
|
|
|
bigints = BigIntegerRangeField(blank=True, null=True)
|
|
|
|
floats = FloatRangeField(blank=True, null=True)
|
|
|
|
timestamps = DateTimeRangeField(blank=True, null=True)
|
|
|
|
dates = DateRangeField(blank=True, null=True)
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2015-01-11 00:14:20 +08:00
|
|
|
|
|
|
|
|
2015-05-31 05:13:58 +08:00
|
|
|
# Only create this model for postgres >= 9.4
|
|
|
|
if connection.vendor == 'postgresql' and connection.pg_version >= 90400:
|
|
|
|
class JSONModel(models.Model):
|
|
|
|
field = JSONField(blank=True, null=True)
|
|
|
|
else:
|
|
|
|
# create an object with this name so we don't have failing imports
|
|
|
|
class JSONModel(object):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-12-21 18:21:37 +08:00
|
|
|
class ArrayFieldSubclass(ArrayField):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(ArrayFieldSubclass, self).__init__(models.IntegerField())
|
2015-02-08 23:21:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AggregateTestModel(models.Model):
|
|
|
|
"""
|
|
|
|
To test postgres-specific general aggregation functions
|
|
|
|
"""
|
|
|
|
char_field = models.CharField(max_length=30, blank=True)
|
|
|
|
integer_field = models.IntegerField(null=True)
|
|
|
|
boolean_field = models.NullBooleanField()
|
|
|
|
|
|
|
|
|
|
|
|
class StatTestModel(models.Model):
|
|
|
|
"""
|
|
|
|
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)
|