| 1 |
from django import VERSION as DJANGO_VERSION |
| 2 |
from django.conf import settings |
| 3 |
from django.contrib.postgres.fields import ( |
| 4 |
ArrayField, HStoreField, |
| 5 |
IntegerRangeField, DateRangeField, |
| 6 |
DateTimeRangeField) |
| 7 |
from django.db.models import ( |
| 8 |
Model, CharField, ForeignKey, BooleanField, DateField, DateTimeField, |
| 9 |
ManyToManyField, BinaryField, IntegerField, GenericIPAddressField, TextChoices, |
| 10 |
FloatField, DecimalField, DurationField, UUIDField, SET_NULL, PROTECT) |
| 11 |
|
| 12 |
|
| 13 |
class SomeChoices(TextChoices): |
| 14 |
foo = 'foo' |
| 15 |
bar = 'bar' |
| 16 |
|
| 17 |
class Test(Model): |
| 18 |
name = CharField(max_length=20) |
| 19 |
owner = ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, |
| 20 |
on_delete=SET_NULL) |
| 21 |
public = BooleanField(default=False) |
| 22 |
date = DateField(null=True, blank=True) |
| 23 |
datetime = DateTimeField(null=True, blank=True) |
| 24 |
permission = ForeignKey('auth.Permission', null=True, blank=True, |
| 25 |
on_delete=PROTECT) |
| 26 |
|
| 27 |
# We can’t use the exact names `float` or `decimal` as database column name |
| 28 |
# since it fails on MySQL. |
| 29 |
a_float = FloatField(null=True, blank=True) |
| 30 |
a_decimal = DecimalField(null=True, blank=True, |
| 31 |
max_digits=5, decimal_places=2) |
| 32 |
a_choice = CharField(max_length=3, choices=SomeChoices.choices, null=True) |
| 33 |
bin = BinaryField(null=True, blank=True) |
| 34 |
ip = GenericIPAddressField(null=True, blank=True) |
| 35 |
duration = DurationField(null=True, blank=True) |
| 36 |
uuid = UUIDField(null=True, blank=True) |
| 37 |
|
| 38 |
try: |
| 39 |
from django.db.models import JSONField |
| 40 |
json = JSONField(null=True, blank=True) |
| 41 |
except ImportError: |
| 42 |
pass |
| 43 |
|
| 44 |
class Meta: |
| 45 |
ordering = ('name',) |
| 46 |
|
| 47 |
|
| 48 |
class TestParent(Model): |
| 49 |
name = CharField(max_length=20) |
| 50 |
|
| 51 |
|
| 52 |
class TestChild(TestParent): |
| 53 |
""" |
| 54 |
A OneToOneField to TestParent is automatically added here. |
| 55 |
https://docs.djangoproject.com/en/3.2/topics/db/models/#multi-table-inheritance |
| 56 |
""" |
| 57 |
public = BooleanField(default=False) |
| 58 |
permissions = ManyToManyField('auth.Permission', blank=True) |
| 59 |
|
| 60 |
|
| 61 |
class PostgresModel(Model): |
| 62 |
int_array = ArrayField(IntegerField(null=True, blank=True), size=3, |
| 63 |
null=True, blank=True) |
| 64 |
|
| 65 |
hstore = HStoreField(null=True, blank=True) |
| 66 |
if DJANGO_VERSION < (4, 0): |
| 67 |
from django.contrib.postgres.fields import JSONField |
| 68 |
json = JSONField(null=True, blank=True) |
| 69 |
|
| 70 |
int_range = IntegerRangeField(null=True, blank=True) |
| 71 |
try: |
| 72 |
from django.contrib.postgres.fields import FloatRangeField |
| 73 |
float_range = FloatRangeField(null=True, blank=True) |
| 74 |
except ImportError: |
| 75 |
pass |
| 76 |
|
| 77 |
try: |
| 78 |
from django.contrib.postgres.fields import DecimalRangeField |
| 79 |
decimal_range = DecimalRangeField(null=True, blank=True) |
| 80 |
except ImportError: |
| 81 |
pass |
| 82 |
date_range = DateRangeField(null=True, blank=True) |
| 83 |
datetime_range = DateTimeRangeField(null=True, blank=True) |
| 84 |
|
| 85 |
class Meta: |
| 86 |
# Tests schema name in table name. |
| 87 |
db_table = '"public"."cachalot_postgresmodel"' |
| 88 |
|
| 89 |
|
| 90 |
class UnmanagedModel(Model): |
| 91 |
name = CharField(max_length=50) |
| 92 |
|
| 93 |
class Meta: |
| 94 |
managed = False |