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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
#!/usr/bin/env python
# https://docs.djangoproject.com/en/3.0/ref/models/fields
import django
from django.conf import settings
from django.db import models
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django_dynamic_fixture.models_sample_app import *
from django_dynamic_fixture.models_third_party import *
class EmptyModel(models.Model):
class Meta:
app_label = 'django_dynamic_fixture'
class ModelWithNumbers(models.Model):
#id is a models.AutoField()
integer = models.IntegerField(unique=True)
smallinteger = models.SmallIntegerField(unique=True)
positiveinteger = models.PositiveIntegerField(unique=True)
positivesmallinteger = models.PositiveSmallIntegerField(unique=True)
biginteger = models.BigIntegerField(unique=True)
float = models.FloatField(unique=True)
decimal = models.DecimalField(max_digits=2, decimal_places=1, unique=False)
class Meta:
verbose_name = 'Numbers'
app_label = 'django_dynamic_fixture'
class ModelWithStrings(models.Model):
char = models.CharField(max_length=1, unique=True)
string = models.CharField(max_length=50, unique=True)
text = models.TextField(unique=True)
slug = models.SlugField(unique=True)
commaseparated = models.CommaSeparatedIntegerField(max_length=100, unique=True)
class Meta:
verbose_name = 'Strings'
app_label = 'django_dynamic_fixture'
class ModelWithBooleans(models.Model):
# https://docs.djangoproject.com/en/1.6/ref/models/fields/#booleanfield
# Django 1.6 changed the default value of BooleanField from False to None
boolean = models.BooleanField(default=False)
nullboolean = models.NullBooleanField()
class Meta:
verbose_name = 'Booleans'
app_label = 'django_dynamic_fixture'
class ModelWithDateTimes(models.Model):
date = models.DateField(unique=True)
datetime = models.DateTimeField(unique=True)
time = models.TimeField(unique=True)
class Meta:
verbose_name = 'DateTimes'
app_label = 'django_dynamic_fixture'
class ModelWithBinary(models.Model):
binary = models.BinaryField()
class Meta:
app_label = 'django_dynamic_fixture'
class ModelWithFieldsWithCustomValidation(models.Model):
email = models.EmailField(unique=True)
url = models.URLField(unique=True)
ip = models.IPAddressField(unique=False)
ipv6 = models.GenericIPAddressField(unique=False)
class Meta:
verbose_name = 'Custom validation'
app_label = 'django_dynamic_fixture'
class ModelWithFileFields(models.Model):
filepath = models.FilePathField(unique=True, blank=True)
file = models.FileField(upload_to='.')
try:
import pil
# just test it if the PIL package is installed
image = models.ImageField(upload_to='.')
except ImportError:
pass
class Meta:
verbose_name = 'File fields'
app_label = 'django_dynamic_fixture'
class ModelWithDefaultValues(models.Model):
integer_with_default = models.IntegerField(default=3)
string_with_choices = models.CharField(max_length=5, choices=(('a', 'A'), ('b', 'B')))
string_with_choices_and_default = models.CharField(max_length=5, default='b', choices=(('a', 'A'), ('b', 'B')))
string_with_optgroup_choices = models.CharField(max_length=5, choices=(('group1', (('a', 'A'), ('b', 'B'))), ('group2', (('c', 'C'), ('d', 'D')))))
foreign_key_with_default = models.ForeignKey(EmptyModel, null=True, default=None, on_delete=models.DO_NOTHING)
class Meta:
verbose_name = 'Default values'
app_label = 'django_dynamic_fixture'
class ModelForNullable(models.Model):
nullable = models.IntegerField(null=True)
not_nullable = models.IntegerField(null=False)
class Meta:
verbose_name = 'Nullable'
app_label = 'django_dynamic_fixture'
class ModelForIgnoreList2(models.Model):
nullable = models.IntegerField(null=True)
non_nullable = models.IntegerField()
class Meta:
verbose_name = 'Ignore list 2'
app_label = 'django_dynamic_fixture'
class ModelForIgnoreList(models.Model):
required = models.IntegerField(null=False)
required_with_default = models.IntegerField(null=False, default=1)
not_required = models.IntegerField(null=True)
not_required_with_default = models.IntegerField(default=1)
self_reference = models.ForeignKey('ModelForIgnoreList', on_delete=models.DO_NOTHING, null=True)
different_reference = models.ForeignKey(ModelForIgnoreList2, on_delete=models.DO_NOTHING)
class Meta:
verbose_name = 'Ignore list'
app_label = 'django_dynamic_fixture'
class ModelRelated(models.Model):
selfforeignkey = models.ForeignKey('self', on_delete=models.DO_NOTHING, null=True, blank=True)
integer = models.IntegerField(null=True)
integer_b = models.IntegerField(null=True)
class Meta:
verbose_name = 'Related'
app_label = 'django_dynamic_fixture'
class ModelRelatedThrough(models.Model):
related = models.ForeignKey('ModelRelated', on_delete=models.DO_NOTHING)
relationship = models.ForeignKey('ModelWithRelationships', on_delete=models.DO_NOTHING)
class Meta:
app_label = 'django_dynamic_fixture'
def default_fk_value():
try:
return ModelRelated.objects.get(id=1)
except ModelRelated.DoesNotExist:
ModelRelated.objects.create()
return ModelRelated.objects.all()[0]
def default_fk_id():
return default_fk_value().pk
class ModelWithRelationships(models.Model):
# relationship
selfforeignkey = models.ForeignKey('self', on_delete=models.DO_NOTHING, null=True, blank=True)
foreignkey = models.ForeignKey('ModelRelated', related_name='fk', on_delete=models.DO_NOTHING)
onetoone = models.OneToOneField('ModelRelated', related_name='o2o', on_delete=models.DO_NOTHING)
manytomany = models.ManyToManyField('ModelRelated', related_name='m2m')
manytomany_through = models.ManyToManyField('ModelRelated', related_name='m2m_through', through=ModelRelatedThrough)
foreignkey_with_default = models.ForeignKey('ModelRelated', related_name='fk2', default=default_fk_value, on_delete=models.DO_NOTHING)
foreignkey_with_id_default = models.ForeignKey('ModelRelated', related_name='fk3', default=default_fk_id, on_delete=models.DO_NOTHING)
integer = models.IntegerField(null=True)
integer_b = models.IntegerField(null=True)
# generic field
# TODO
class Meta:
verbose_name = 'Relationships'
app_label = 'django_dynamic_fixture'
class ModelWithCyclicDependency(models.Model):
model_b = models.ForeignKey('ModelWithCyclicDependency2', on_delete=models.DO_NOTHING, null=True)
class Meta:
verbose_name = 'Cyclic dependency'
app_label = 'django_dynamic_fixture'
class ModelWithCyclicDependency2(models.Model):
model_a = models.ForeignKey(ModelWithCyclicDependency, on_delete=models.DO_NOTHING, null=True)
class Meta:
verbose_name = 'Cyclic dependency 2'
app_label = 'django_dynamic_fixture'
class ModelAbstract(models.Model):
integer = models.IntegerField(unique=True)
class Meta:
abstract = True
verbose_name = 'Abstract'
app_label = 'django_dynamic_fixture'
class ModelParent(ModelAbstract):
class Meta:
verbose_name = 'Parent'
app_label = 'django_dynamic_fixture'
class ModelChild(ModelParent):
class Meta:
verbose_name = 'Child'
app_label = 'django_dynamic_fixture'
class ModelChildWithCustomParentLink(ModelParent):
my_custom_ref = models.OneToOneField(ModelParent, parent_link=True, related_name='my_custom_ref_x', on_delete=models.DO_NOTHING)
class Meta:
verbose_name = 'Custom child'
app_label = 'django_dynamic_fixture'
class ModelWithRefToParent(models.Model):
parent = models.ForeignKey(ModelParent, on_delete=models.DO_NOTHING)
class Meta:
verbose_name = 'Child with parent'
app_label = 'django_dynamic_fixture'
class CustomDjangoField(models.IntegerField):
pass
class CustomDjangoField2(models.IntegerField):
pass
class CustomDjangoFieldMixin:
pass
class CustomDjangoFieldMultipleInheritance(CustomDjangoFieldMixin, models.IntegerField):
pass
class NewField(models.Field):
# Avoid OperationalError("table has no column named ...") errors
def db_type(self, connection):
return 'char(25)'
class ModelWithCustomFields(models.Model):
x = CustomDjangoField(null=False)
y = NewField(null=True)
class Meta:
verbose_name = 'Custom fields'
app_label = 'django_dynamic_fixture'
class ModelWithCustomFieldsMultipleInheritance(models.Model):
x = CustomDjangoFieldMultipleInheritance(null=False)
y = NewField(null=True)
class Meta:
verbose_name = 'Custom fields with multiple inheritance'
app_label = 'django_dynamic_fixture'
class ModelWithUnsupportedField(models.Model):
z = NewField(null=False)
class Meta:
verbose_name = 'Unsupported field'
app_label = 'django_dynamic_fixture'
class ModelWithValidators(models.Model):
field_validator = models.CharField(max_length=3, validators=[RegexValidator(regex=r'ok')])
clean_validator = models.CharField(max_length=3)
class Meta:
verbose_name = 'Validators'
app_label = 'django_dynamic_fixture'
def clean(self):
if self.clean_validator != 'ok':
raise ValidationError('ops')
class ModelWithAutoDateTimes(models.Model):
auto_now_add = models.DateField(auto_now_add=True)
auto_now = models.DateField(auto_now=True)
manytomany = models.ManyToManyField('ModelWithAutoDateTimes', related_name='m2m')
class Meta:
verbose_name = 'Auto DateTime'
app_label = 'django_dynamic_fixture'
class ModelForCopy2(models.Model):
int_e = models.IntegerField()
class Meta:
verbose_name = 'Copy 2'
app_label = 'django_dynamic_fixture'
class ModelForCopy(models.Model):
int_a = models.IntegerField()
int_b = models.IntegerField(null=None)
int_c = models.IntegerField()
int_d = models.IntegerField()
e = models.ForeignKey(ModelForCopy2, on_delete=models.DO_NOTHING)
class Meta:
verbose_name = 'Copy'
app_label = 'django_dynamic_fixture'
class ModelForLibrary2(models.Model):
integer = models.IntegerField(null=True)
integer_unique = models.IntegerField(unique=True)
class Meta:
verbose_name = 'Library 2'
app_label = 'django_dynamic_fixture'
class ModelForLibrary(models.Model):
integer = models.IntegerField(null=True)
integer_unique = models.IntegerField(unique=True)
selfforeignkey = models.ForeignKey('self', on_delete=models.DO_NOTHING, null=True, blank=True)
foreignkey = models.ForeignKey('ModelForLibrary2', related_name='fk', on_delete=models.DO_NOTHING)
class Meta:
verbose_name = 'Library'
app_label = 'django_dynamic_fixture'
class ProxyModelForLibrary(ModelForLibrary):
class Meta:
proxy = True
verbose_name = 'Proxy Library'
app_label = 'django_dynamic_fixture'
class ModelWithUniqueCharField(models.Model):
text_unique = models.CharField(max_length=20, unique=True)
class Meta:
verbose_name = 'Unique char field'
app_label = 'django_dynamic_fixture'
class ModelWithClean(models.Model):
integer = models.IntegerField()
class Meta:
verbose_name = 'Clean'
app_label = 'django_dynamic_fixture'
def clean(self):
if self.integer != 9999: # just for testing
raise ValidationError('integer is not 9999')
class ModelForSignals(models.Model):
class Meta:
verbose_name = 'Signals'
app_label = 'django_dynamic_fixture'
class ModelForSignals2(models.Model):
class Meta:
verbose_name = 'Signals 2'
app_label = 'django_dynamic_fixture'
class ModelForFieldPlugins(models.Model):
# aaa = CustomDjangoField(null=False) # defined in settings.py
# bbb = models.IntegerField(null=False)
custom_field_custom_fixture = CustomDjangoField(null=False) # defined in settings.py
custom_field_custom_fixture2 = CustomDjangoField2(null=False) # defined in settings.py
class Meta:
app_label = 'django_dynamic_fixture'
class ModelWithCommonNames(models.Model):
instance = models.IntegerField(null=False)
field = models.IntegerField(null=False)
class Meta:
app_label = 'django_dynamic_fixture'
class ModelWithNamedPrimaryKey(models.Model):
named_pk = models.AutoField(primary_key=True)
if (hasattr(settings, 'DDF_TEST_GEODJANGO') and settings.DDF_TEST_GEODJANGO):
from django.contrib.gis.db import models as geomodels
class ModelForGeoDjango(geomodels.Model):
geometry = geomodels.GeometryField()
point = geomodels.PointField()
line_string = geomodels.LineStringField()
polygon = geomodels.PolygonField()
multi_point = geomodels.MultiPointField()
multi_line_string = geomodels.MultiLineStringField()
multi_polygon = geomodels.MultiPolygonField()
geometry_collection = geomodels.GeometryCollectionField()
class Meta:
app_label = 'django_dynamic_fixture'
class ModelForUUID(models.Model):
uuid = models.UUIDField()
class Meta:
app_label = 'django_dynamic_fixture'
|