summaryrefslogtreecommitdiff
path: root/tests/model_fields/models.py
diff options
context:
space:
mode:
authordjango-bot <ops@djangoproject.com>2022-02-03 20:24:19 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-02-07 20:37:05 +0100
commit9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch)
treef0506b668a013d0063e5fba3dbf4863b466713ba /tests/model_fields/models.py
parentf68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff)
downloaddjango-9c19aff7c7561e3a82978a272ecdaad40dda5c00.tar.gz
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'tests/model_fields/models.py')
-rw-r--r--tests/model_fields/models.py142
1 files changed, 82 insertions, 60 deletions
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py
index 9e8baeb565..647916e360 100644
--- a/tests/model_fields/models.py
+++ b/tests/model_fields/models.py
@@ -3,9 +3,7 @@ import os
import tempfile
import uuid
-from django.contrib.contenttypes.fields import (
- GenericForeignKey, GenericRelation,
-)
+from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.core.files.storage import FileSystemStorage
from django.core.serializers.json import DjangoJSONEncoder
@@ -30,23 +28,27 @@ def get_foo():
class Bar(models.Model):
b = models.CharField(max_length=10)
- a = models.ForeignKey(Foo, models.CASCADE, default=get_foo, related_name='bars')
+ a = models.ForeignKey(Foo, models.CASCADE, default=get_foo, related_name="bars")
class Whiz(models.Model):
CHOICES = (
- ('Group 1', (
- (1, 'First'),
- (2, 'Second'),
- )
+ (
+ "Group 1",
+ (
+ (1, "First"),
+ (2, "Second"),
+ ),
),
- ('Group 2', (
- (3, 'Third'),
- (4, 'Fourth'),
- )
+ (
+ "Group 2",
+ (
+ (3, "Third"),
+ (4, "Fourth"),
+ ),
),
- (0, 'Other'),
- (5, _('translated')),
+ (0, "Other"),
+ (5, _("translated")),
)
c = models.IntegerField(choices=CHOICES, null=True)
@@ -56,7 +58,7 @@ class WhizDelayed(models.Model):
# Contrived way of adding choices later.
-WhizDelayed._meta.get_field('c').choices = Whiz.CHOICES
+WhizDelayed._meta.get_field("c").choices = Whiz.CHOICES
class WhizIter(models.Model):
@@ -70,7 +72,7 @@ class WhizIterEmpty(models.Model):
class Choiceful(models.Model):
no_choices = models.IntegerField(null=True)
empty_choices = models.IntegerField(choices=(), null=True)
- with_choices = models.IntegerField(choices=[(1, 'A')], null=True)
+ with_choices = models.IntegerField(choices=[(1, "A")], null=True)
empty_choices_bool = models.BooleanField(choices=())
empty_choices_text = models.TextField(choices=())
@@ -139,7 +141,7 @@ class NullBooleanModel(models.Model):
class BooleanModel(models.Model):
bfield = models.BooleanField()
- string = models.CharField(max_length=10, default='abc')
+ string = models.CharField(max_length=10, default="abc")
class DateTimeModel(models.Model):
@@ -162,17 +164,19 @@ class PrimaryKeyCharModel(models.Model):
class FksToBooleans(models.Model):
"""Model with FKs to models with {Null,}BooleanField's, #15040"""
+
bf = models.ForeignKey(BooleanModel, models.CASCADE)
nbf = models.ForeignKey(NullBooleanModel, models.CASCADE)
class FkToChar(models.Model):
"""Model with FK to a model with a CharField primary key, #19299"""
+
out = models.ForeignKey(PrimaryKeyCharModel, models.CASCADE)
class RenamedField(models.Model):
- modelname = models.IntegerField(name="fieldname", choices=((1, 'One'),))
+ modelname = models.IntegerField(name="fieldname", choices=((1, "One"),))
class VerboseNameField(models.Model):
@@ -203,7 +207,7 @@ class VerboseNameField(models.Model):
class GenericIPAddress(models.Model):
- ip = models.GenericIPAddressField(null=True, protocol='ipv4')
+ ip = models.GenericIPAddressField(null=True, protocol="ipv4")
###############################################################################
@@ -219,19 +223,21 @@ class DecimalLessThanOne(models.Model):
class FieldClassAttributeModel(models.Model):
field_class = models.CharField
+
###############################################################################
class DataModel(models.Model):
- short_data = models.BinaryField(max_length=10, default=b'\x08')
+ short_data = models.BinaryField(max_length=10, default=b"\x08")
data = models.BinaryField()
+
###############################################################################
# FileField
class Document(models.Model):
- myfile = models.FileField(upload_to='unused', unique=True)
+ myfile = models.FileField(upload_to="unused", unique=True)
###############################################################################
@@ -239,11 +245,13 @@ class Document(models.Model):
# If Pillow available, do these tests.
if Image:
+
class TestImageFieldFile(ImageFieldFile):
"""
Custom Field File class that records whether or not the underlying file
was opened.
"""
+
def __init__(self, *args, **kwargs):
self.was_opened = False
super().__init__(*args, **kwargs)
@@ -258,14 +266,15 @@ if Image:
# Set up a temp directory for file storage.
temp_storage_dir = tempfile.mkdtemp()
temp_storage = FileSystemStorage(temp_storage_dir)
- temp_upload_to_dir = os.path.join(temp_storage.location, 'tests')
+ temp_upload_to_dir = os.path.join(temp_storage.location, "tests")
class Person(models.Model):
"""
Model that defines an ImageField with no dimension fields.
"""
+
name = models.CharField(max_length=50)
- mugshot = TestImageField(storage=temp_storage, upload_to='tests')
+ mugshot = TestImageField(storage=temp_storage, upload_to="tests")
class AbstractPersonWithHeight(models.Model):
"""
@@ -273,8 +282,10 @@ if Image:
to make sure the dimension update is correctly run on concrete subclass
instance post-initialization.
"""
- mugshot = TestImageField(storage=temp_storage, upload_to='tests',
- height_field='mugshot_height')
+
+ mugshot = TestImageField(
+ storage=temp_storage, upload_to="tests", height_field="mugshot_height"
+ )
mugshot_height = models.PositiveSmallIntegerField()
class Meta:
@@ -285,16 +296,21 @@ if Image:
Concrete model that subclass an abstract one with only on dimension
field.
"""
+
name = models.CharField(max_length=50)
class PersonWithHeightAndWidth(models.Model):
"""
Model that defines height and width fields after the ImageField.
"""
+
name = models.CharField(max_length=50)
- mugshot = TestImageField(storage=temp_storage, upload_to='tests',
- height_field='mugshot_height',
- width_field='mugshot_width')
+ mugshot = TestImageField(
+ storage=temp_storage,
+ upload_to="tests",
+ height_field="mugshot_height",
+ width_field="mugshot_width",
+ )
mugshot_height = models.PositiveSmallIntegerField()
mugshot_width = models.PositiveSmallIntegerField()
@@ -302,12 +318,16 @@ if Image:
"""
Model that defines height and width fields before the ImageField.
"""
+
name = models.CharField(max_length=50)
mugshot_height = models.PositiveSmallIntegerField()
mugshot_width = models.PositiveSmallIntegerField()
- mugshot = TestImageField(storage=temp_storage, upload_to='tests',
- height_field='mugshot_height',
- width_field='mugshot_width')
+ mugshot = TestImageField(
+ storage=temp_storage,
+ upload_to="tests",
+ height_field="mugshot_height",
+ width_field="mugshot_width",
+ )
class PersonTwoImages(models.Model):
"""
@@ -316,20 +336,26 @@ if Image:
* Defines the height/width fields before the ImageFields
* Has a nullable ImageField
"""
+
name = models.CharField(max_length=50)
mugshot_height = models.PositiveSmallIntegerField()
mugshot_width = models.PositiveSmallIntegerField()
- mugshot = TestImageField(storage=temp_storage, upload_to='tests',
- height_field='mugshot_height',
- width_field='mugshot_width')
- headshot_height = models.PositiveSmallIntegerField(
- blank=True, null=True)
- headshot_width = models.PositiveSmallIntegerField(
- blank=True, null=True)
- headshot = TestImageField(blank=True, null=True,
- storage=temp_storage, upload_to='tests',
- height_field='headshot_height',
- width_field='headshot_width')
+ mugshot = TestImageField(
+ storage=temp_storage,
+ upload_to="tests",
+ height_field="mugshot_height",
+ width_field="mugshot_width",
+ )
+ headshot_height = models.PositiveSmallIntegerField(blank=True, null=True)
+ headshot_width = models.PositiveSmallIntegerField(blank=True, null=True)
+ headshot = TestImageField(
+ blank=True,
+ null=True,
+ storage=temp_storage,
+ upload_to="tests",
+ height_field="headshot_height",
+ width_field="headshot_width",
+ )
class CustomJSONDecoder(json.JSONDecoder):
@@ -337,8 +363,8 @@ class CustomJSONDecoder(json.JSONDecoder):
return super().__init__(object_hook=self.as_uuid, *args, **kwargs)
def as_uuid(self, dct):
- if 'uuid' in dct:
- dct['uuid'] = uuid.UUID(dct['uuid'])
+ if "uuid" in dct:
+ dct["uuid"] = uuid.UUID(dct["uuid"])
return dct
@@ -346,7 +372,7 @@ class JSONModel(models.Model):
value = models.JSONField()
class Meta:
- required_db_features = {'supports_json_field'}
+ required_db_features = {"supports_json_field"}
class NullableJSONModel(models.Model):
@@ -358,7 +384,7 @@ class NullableJSONModel(models.Model):
)
class Meta:
- required_db_features = {'supports_json_field'}
+ required_db_features = {"supports_json_field"}
class RelatedJSONModel(models.Model):
@@ -366,7 +392,7 @@ class RelatedJSONModel(models.Model):
json_model = models.ForeignKey(NullableJSONModel, models.CASCADE)
class Meta:
- required_db_features = {'supports_json_field'}
+ required_db_features = {"supports_json_field"}
class AllFieldsModel(models.Model):
@@ -393,19 +419,15 @@ class AllFieldsModel(models.Model):
uuid = models.UUIDField()
fo = models.ForeignObject(
- 'self',
+ "self",
on_delete=models.CASCADE,
- from_fields=['positive_integer'],
- to_fields=['id'],
- related_name='reverse'
- )
- fk = models.ForeignKey(
- 'self',
- models.CASCADE,
- related_name='reverse2'
+ from_fields=["positive_integer"],
+ to_fields=["id"],
+ related_name="reverse",
)
- m2m = models.ManyToManyField('self')
- oto = models.OneToOneField('self', models.CASCADE)
+ fk = models.ForeignKey("self", models.CASCADE, related_name="reverse2")
+ m2m = models.ManyToManyField("self")
+ oto = models.OneToOneField("self", models.CASCADE)
object_id = models.PositiveIntegerField()
content_type = models.ForeignKey(ContentType, models.CASCADE)
@@ -414,7 +436,7 @@ class AllFieldsModel(models.Model):
class ManyToMany(models.Model):
- m2m = models.ManyToManyField('self')
+ m2m = models.ManyToManyField("self")
###############################################################################
@@ -433,7 +455,7 @@ class PrimaryKeyUUIDModel(models.Model):
class RelatedToUUIDModel(models.Model):
- uuid_fk = models.ForeignKey('PrimaryKeyUUIDModel', models.CASCADE)
+ uuid_fk = models.ForeignKey("PrimaryKeyUUIDModel", models.CASCADE)
class UUIDChild(PrimaryKeyUUIDModel):