1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
from typing import TYPE_CHECKING, Optional
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.db import models
from strawberry_django.descriptors import model_property
if TYPE_CHECKING:
from django.db.models.manager import RelatedManager
def validate_fruit_type(value: str):
if "rotten" in value:
raise ValidationError("We do not allow rotten fruits.")
class NameDescriptionMixin(models.Model):
name = models.CharField(max_length=20)
description = models.TextField()
class Meta:
abstract = True
class Vegetable(NameDescriptionMixin):
world_production = models.FloatField()
class Fruit(models.Model):
id: Optional[int]
name = models.CharField(max_length=20)
color_id: Optional[int]
color = models.ForeignKey(
"Color",
null=True,
blank=True,
related_name="fruits",
on_delete=models.CASCADE,
)
types = models.ManyToManyField("FruitType", related_name="fruits")
sweetness = models.IntegerField(
default=5,
help_text="Level of sweetness, from 1 to 10",
)
picture = models.ImageField(
null=True,
blank=True,
default=None,
upload_to=".tmp_upload",
)
def name_upper(self):
return self.name.upper()
@property
def name_lower(self):
return self.name.lower()
@model_property
def name_length(self) -> int:
return len(self.name)
class TomatoWithRequiredPicture(models.Model):
name = models.CharField(max_length=20)
picture = models.ImageField(
null=False,
blank=False,
upload_to=".tmp_upload",
)
class Color(models.Model):
fruits: "RelatedManager[Fruit]"
name = models.CharField(max_length=20)
class FruitType(models.Model):
id: Optional[int]
name = models.CharField(max_length=20, validators=[validate_fruit_type])
class User(models.Model):
name = models.CharField(max_length=50)
group_id: Optional[int]
group = models.ForeignKey(
"Group",
null=True,
blank=True,
related_name="users",
on_delete=models.CASCADE,
)
tag = models.OneToOneField("Tag", null=True, on_delete=models.CASCADE)
@property
def group_prop(self) -> Optional["Group"]:
return self.group
def get_group(self) -> Optional["Group"]:
return self.group
class Group(models.Model):
users: "RelatedManager[User]"
name = models.CharField(max_length=50)
tags = models.ManyToManyField("Tag", null=True, related_name="groups")
class Tag(models.Model):
name = models.CharField(max_length=50)
class Book(models.Model):
"""Model with lots of extra metadata."""
title = models.CharField(
max_length=20,
blank=False,
null=False,
help_text="The name by which the book is known.",
)
try:
from django.contrib.gis.db import models as geos_fields
GEOS_IMPORTED = True
class GeosFieldsModel(models.Model):
point = geos_fields.PointField(null=True, blank=True)
line_string = geos_fields.LineStringField(null=True, blank=True)
polygon = geos_fields.PolygonField(null=True, blank=True)
multi_point = geos_fields.MultiPointField(null=True, blank=True)
multi_line_string = geos_fields.MultiLineStringField(null=True, blank=True)
multi_polygon = geos_fields.MultiPolygonField(null=True, blank=True)
geometry = geos_fields.GeometryField(null=True, blank=True)
except ImproperlyConfigured:
GEOS_IMPORTED = False
|