2006-06-28 16:00:37 +00:00
|
|
|
"""
|
2015-09-26 17:12:30 +00:00
|
|
|
******** Models for test_data.py ***********
|
|
|
|
The following classes are for testing basic data marshalling, including
|
|
|
|
NULL values, where allowed.
|
|
|
|
The basic idea is to have a model for each Django data type.
|
2006-06-28 16:00:37 +00:00
|
|
|
"""
|
2019-12-12 03:34:28 +00:00
|
|
|
import uuid
|
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2006-06-28 16:00:37 +00:00
|
|
|
from django.db import models
|
2010-09-19 14:04:34 +00:00
|
|
|
|
2015-09-26 17:12:30 +00:00
|
|
|
from .base import BaseModel
|
2007-01-27 13:42:51 +00:00
|
|
|
|
2009-04-13 12:35:49 +00:00
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
class BinaryData(models.Model):
|
|
|
|
data = models.BinaryField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class BooleanData(models.Model):
|
2017-05-06 14:56:28 +00:00
|
|
|
data = models.BooleanField(default=False, null=True)
|
2015-09-26 12:14:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CharData(models.Model):
|
|
|
|
data = models.CharField(max_length=30, null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class DateData(models.Model):
|
|
|
|
data = models.DateField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class DateTimeData(models.Model):
|
|
|
|
data = models.DateTimeField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class DecimalData(models.Model):
|
|
|
|
data = models.DecimalField(null=True, decimal_places=3, max_digits=5)
|
|
|
|
|
|
|
|
|
|
|
|
class EmailData(models.Model):
|
|
|
|
data = models.EmailField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class FileData(models.Model):
|
2016-11-30 22:05:31 +00:00
|
|
|
data = models.FileField(null=True)
|
2015-09-26 12:14:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FilePathData(models.Model):
|
|
|
|
data = models.FilePathField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class FloatData(models.Model):
|
|
|
|
data = models.FloatField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class IntegerData(models.Model):
|
|
|
|
data = models.IntegerField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class BigIntegerData(models.Model):
|
|
|
|
data = models.BigIntegerField(null=True)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
# class ImageData(models.Model):
|
|
|
|
# data = models.ImageField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class GenericIPAddressData(models.Model):
|
|
|
|
data = models.GenericIPAddressField(null=True)
|
|
|
|
|
|
|
|
|
2019-10-16 12:32:12 +00:00
|
|
|
class PositiveBigIntegerData(models.Model):
|
|
|
|
data = models.PositiveBigIntegerField(null=True)
|
|
|
|
|
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
class PositiveIntegerData(models.Model):
|
|
|
|
data = models.PositiveIntegerField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class PositiveSmallIntegerData(models.Model):
|
|
|
|
data = models.PositiveSmallIntegerField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class SlugData(models.Model):
|
|
|
|
data = models.SlugField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class SmallData(models.Model):
|
|
|
|
data = models.SmallIntegerField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class TextData(models.Model):
|
|
|
|
data = models.TextField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class TimeData(models.Model):
|
|
|
|
data = models.TimeField(null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class Tag(models.Model):
|
|
|
|
"""A tag on an item."""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
data = models.SlugField()
|
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
|
|
|
object_id = models.PositiveIntegerField()
|
|
|
|
|
|
|
|
content_object = GenericForeignKey()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["data"]
|
|
|
|
|
|
|
|
|
|
|
|
class GenericData(models.Model):
|
|
|
|
data = models.CharField(max_length=30)
|
|
|
|
|
|
|
|
tags = GenericRelation(Tag)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
# The following test classes are all for validation
|
|
|
|
# of related objects; in particular, forward, backward,
|
|
|
|
# and self references.
|
|
|
|
|
|
|
|
|
|
|
|
class Anchor(models.Model):
|
|
|
|
"""This is a model that can be used as
|
|
|
|
something for other models to point at"""
|
|
|
|
|
|
|
|
data = models.CharField(max_length=30)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ("id",)
|
|
|
|
|
|
|
|
|
|
|
|
class UniqueAnchor(models.Model):
|
|
|
|
"""This is a model that can be used as
|
|
|
|
something for other models to point at"""
|
|
|
|
|
|
|
|
data = models.CharField(unique=True, max_length=30)
|
|
|
|
|
|
|
|
|
|
|
|
class FKData(models.Model):
|
|
|
|
data = models.ForeignKey(Anchor, models.SET_NULL, null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class M2MData(models.Model):
|
|
|
|
data = models.ManyToManyField(Anchor)
|
|
|
|
|
|
|
|
|
|
|
|
class O2OData(models.Model):
|
|
|
|
# One to one field can't be null here, since it is a PK.
|
|
|
|
data = models.OneToOneField(Anchor, models.CASCADE, primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class FKSelfData(models.Model):
|
|
|
|
data = models.ForeignKey("self", models.CASCADE, null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class M2MSelfData(models.Model):
|
|
|
|
data = models.ManyToManyField("self", symmetrical=False)
|
|
|
|
|
|
|
|
|
|
|
|
class FKDataToField(models.Model):
|
|
|
|
data = models.ForeignKey(UniqueAnchor, models.SET_NULL, null=True, to_field="data")
|
|
|
|
|
|
|
|
|
|
|
|
class FKDataToO2O(models.Model):
|
|
|
|
data = models.ForeignKey(O2OData, models.SET_NULL, null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class M2MIntermediateData(models.Model):
|
|
|
|
data = models.ManyToManyField(Anchor, through="Intermediate")
|
|
|
|
|
|
|
|
|
|
|
|
class Intermediate(models.Model):
|
|
|
|
left = models.ForeignKey(M2MIntermediateData, models.CASCADE)
|
|
|
|
right = models.ForeignKey(Anchor, models.CASCADE)
|
|
|
|
extra = models.CharField(max_length=30, blank=True, default="doesn't matter")
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
# The following test classes are for validating the
|
|
|
|
# deserialization of objects that use a user-defined
|
|
|
|
# field as the primary key.
|
|
|
|
# Some of these data types have been commented out
|
|
|
|
# because they can't be used as a primary key on one
|
|
|
|
# or all database backends.
|
|
|
|
|
|
|
|
|
|
|
|
class BooleanPKData(models.Model):
|
|
|
|
data = models.BooleanField(primary_key=True, default=False)
|
|
|
|
|
|
|
|
|
|
|
|
class CharPKData(models.Model):
|
|
|
|
data = models.CharField(max_length=30, primary_key=True)
|
|
|
|
|
|
|
|
|
2017-06-05 16:59:22 +00:00
|
|
|
class DatePKData(models.Model):
|
|
|
|
data = models.DateField(primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class DateTimePKData(models.Model):
|
|
|
|
data = models.DateTimeField(primary_key=True)
|
2015-09-26 12:14:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DecimalPKData(models.Model):
|
|
|
|
data = models.DecimalField(primary_key=True, decimal_places=3, max_digits=5)
|
|
|
|
|
|
|
|
|
|
|
|
class EmailPKData(models.Model):
|
|
|
|
data = models.EmailField(primary_key=True)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
# class FilePKData(models.Model):
|
2016-11-30 22:05:31 +00:00
|
|
|
# data = models.FileField(primary_key=True)
|
2015-09-26 12:14:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FilePathPKData(models.Model):
|
|
|
|
data = models.FilePathField(primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class FloatPKData(models.Model):
|
|
|
|
data = models.FloatField(primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class IntegerPKData(models.Model):
|
|
|
|
data = models.IntegerField(primary_key=True)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
# class ImagePKData(models.Model):
|
|
|
|
# data = models.ImageField(primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class GenericIPAddressPKData(models.Model):
|
|
|
|
data = models.GenericIPAddressField(primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class PositiveIntegerPKData(models.Model):
|
|
|
|
data = models.PositiveIntegerField(primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class PositiveSmallIntegerPKData(models.Model):
|
|
|
|
data = models.PositiveSmallIntegerField(primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class SlugPKData(models.Model):
|
|
|
|
data = models.SlugField(primary_key=True)
|
|
|
|
|
|
|
|
|
|
|
|
class SmallPKData(models.Model):
|
|
|
|
data = models.SmallIntegerField(primary_key=True)
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
# class TextPKData(models.Model):
|
|
|
|
# data = models.TextField(primary_key=True)
|
|
|
|
|
2017-09-18 07:34:21 +00:00
|
|
|
# class TimePKData(models.Model):
|
|
|
|
# data = models.TimeField(primary_key=True)
|
2015-09-26 12:14:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UUIDData(models.Model):
|
|
|
|
data = models.UUIDField(primary_key=True)
|
|
|
|
|
|
|
|
|
2019-12-12 03:34:28 +00:00
|
|
|
class UUIDDefaultData(models.Model):
|
|
|
|
data = models.UUIDField(primary_key=True, default=uuid.uuid4)
|
|
|
|
|
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
class FKToUUID(models.Model):
|
|
|
|
data = models.ForeignKey(UUIDData, models.CASCADE)
|
|
|
|
|
|
|
|
|
|
|
|
# Tests for handling fields with pre_save functions, or
|
|
|
|
# models with save functions that modify data
|
|
|
|
|
|
|
|
|
|
|
|
class AutoNowDateTimeData(models.Model):
|
|
|
|
data = models.DateTimeField(null=True, auto_now=True)
|
|
|
|
|
|
|
|
|
|
|
|
class ModifyingSaveData(models.Model):
|
|
|
|
data = models.IntegerField(null=True)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
A save method that modifies the data in the object.
|
2016-10-27 07:53:39 +00:00
|
|
|
A user-defined save() method isn't called when objects are deserialized
|
|
|
|
(#4459).
|
2015-09-26 12:14:23 +00:00
|
|
|
"""
|
|
|
|
self.data = 666
|
2017-01-21 13:13:44 +00:00
|
|
|
super().save(*args, **kwargs)
|
2015-09-26 12:14:23 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2015-09-26 12:14:23 +00:00
|
|
|
# Tests for serialization of models using inheritance.
|
|
|
|
# Regression for #7202, #7350
|
|
|
|
|
|
|
|
|
|
|
|
class AbstractBaseModel(models.Model):
|
|
|
|
parent_data = models.IntegerField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
|
|
class InheritAbstractModel(AbstractBaseModel):
|
|
|
|
child_data = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
|
|
class InheritBaseModel(BaseModel):
|
|
|
|
child_data = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
|
|
class ExplicitInheritBaseModel(BaseModel):
|
2016-04-22 16:59:41 +00:00
|
|
|
parent = models.OneToOneField(BaseModel, models.CASCADE, parent_link=True)
|
2015-09-26 12:14:23 +00:00
|
|
|
child_data = models.IntegerField()
|
|
|
|
|
|
|
|
|
|
|
|
class LengthModel(models.Model):
|
|
|
|
data = models.IntegerField()
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self.data
|