def test_create_model_reordering_circular_fk(self): """ CreateModel reordering behavior doesn't result in an infinite loop if there are FKs in both directions. """ self.assertOptimizesTo( [ migrations.CreateModel('Bar', [('url', models.TextField())]), migrations.CreateModel( 'Foo', [('name', models.CharField(max_length=255))]), migrations.AddField( 'Bar', 'foo_fk', models.ForeignKey('migrations.Foo', models.CASCADE)), migrations.AddField( 'Foo', 'bar_fk', models.ForeignKey('migrations.Bar', models.CASCADE)), ], [ migrations.CreateModel( 'Foo', [('name', models.CharField(max_length=255))]), migrations.CreateModel('Bar', [ ('url', models.TextField()), ('foo_fk', models.ForeignKey('migrations.Foo', models.CASCADE)), ]), migrations.AddField( 'Foo', 'bar_fk', models.ForeignKey('migrations.Foo', models.CASCADE)), ], )
def test_max_length_passed_to_formfield(self): """ TextField passes its max_length attribute to form fields created using their formfield() method. """ tf1 = models.TextField() tf2 = models.TextField(max_length=2345) self.assertIsNone(tf1.formfield().max_length) self.assertEqual(2345, tf2.formfield().max_length)
class Comment(models.Model): comment = models.TextField() # Content-object field content_type = models.ForeignKey(ContentType, models.CASCADE) object_pk = models.TextField() content_object = GenericForeignKey(ct_field="content_type", fk_field="object_pk") class Meta: ordering = ['id']
class ArticleTranslation(models.Model): article = models.ForeignKey(Article, models.CASCADE) lang = models.CharField(max_length=2) title = models.CharField(max_length=100) body = models.TextField() abstract = models.TextField(null=True) class Meta: unique_together = ('article', 'lang') ordering = ('active_translation__title', )
def test_serialize_fields(self): self.assertSerializedFieldEqual(models.CharField(max_length=255)) self.assertSerializedResultEqual(models.CharField(max_length=255), ("models.CharField(max_length=255)", {"from djmodels.db import models"})) self.assertSerializedFieldEqual(models.TextField(null=True, blank=True)) self.assertSerializedResultEqual( models.TextField(null=True, blank=True), ("models.TextField(blank=True, null=True)", {'from djmodels.db import models'}))
class Migration(migrations.Migration): dependencies = [] operations = [ migrations.CreateModel( name='CharTextArrayIndexModel', fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('char', djmodels.contrib.postgres.fields.ArrayField( models.CharField(max_length=10), db_index=True, size=100)), ('char2', models.CharField(max_length=11, db_index=True)), ('text', djmodels.contrib.postgres.fields.ArrayField( models.TextField(), db_index=True)), ], options={}, bases=(models.Model, ), ), ]
class VerboseNameField(models.Model): id = models.AutoField("verbose pk", primary_key=True) field1 = models.BigIntegerField("verbose field1") field2 = models.BooleanField("verbose field2", default=False) field3 = models.CharField("verbose field3", max_length=10) field4 = models.DateField("verbose field4") field5 = models.DateTimeField("verbose field5") field6 = models.DecimalField("verbose field6", max_digits=6, decimal_places=1) field7 = models.EmailField("verbose field7") field8 = models.FileField("verbose field8", upload_to="unused") field9 = models.FilePathField("verbose field9") field10 = models.FloatField("verbose field10") # Don't want to depend on Pillow in this test # field_image = models.ImageField("verbose field") field11 = models.IntegerField("verbose field11") field12 = models.GenericIPAddressField("verbose field12", protocol="ipv4") field13 = models.NullBooleanField("verbose field13") field14 = models.PositiveIntegerField("verbose field14") field15 = models.PositiveSmallIntegerField("verbose field15") field16 = models.SlugField("verbose field16") field17 = models.SmallIntegerField("verbose field17") field18 = models.TextField("verbose field18") field19 = models.TimeField("verbose field19") field20 = models.URLField("verbose field20") field21 = models.UUIDField("verbose field21") field22 = models.DurationField("verbose field22")
class FlatPage(models.Model): url = models.CharField(_('URL'), max_length=100, db_index=True) title = models.CharField(_('title'), max_length=200) content = models.TextField(_('content'), blank=True) enable_comments = models.BooleanField(_('enable comments'), default=False) template_name = models.CharField( _('template name'), max_length=70, blank=True, help_text=_( "Example: 'flatpages/contact_page.html'. If this isn't provided, " "the system will use 'flatpages/default.html'."), ) registration_required = models.BooleanField( _('registration required'), help_text= _("If this is checked, only logged-in users will be able to view the page." ), default=False, ) sites = models.ManyToManyField(Site, verbose_name=_('sites')) class Meta: db_table = 'django_flatpage' verbose_name = _('flat page') verbose_name_plural = _('flat pages') ordering = ('url', ) def __str__(self): return "%s -- %s" % (self.url, self.title) def get_absolute_url(self): # Handle script prefix manually because we bypass reverse() return iri_to_uri(get_script_prefix().rstrip('/') + self.url)
class Article(models.Model): name = models.CharField(max_length=50) text = models.TextField() submitted_from = models.GenericIPAddressField(blank=True, null=True) def __str__(self): return "Article %s" % self.name
class Post(models.Model): name = models.CharField(max_length=30) text = models.TextField() tags = GenericRelation('Tag') class Meta: db_table = 'CaseSensitive_Post'
class NullableFields(models.Model): # Fields in db.backends.oracle.BulkInsertMapper big_int_filed = models.BigIntegerField(null=True, default=1) binary_field = models.BinaryField(null=True, default=b'data') date_field = models.DateField(null=True, default=timezone.now) datetime_field = models.DateTimeField(null=True, default=timezone.now) decimal_field = models.DecimalField(null=True, max_digits=2, decimal_places=1, default=Decimal('1.1')) duration_field = models.DurationField(null=True, default=datetime.timedelta(1)) float_field = models.FloatField(null=True, default=3.2) integer_field = models.IntegerField(null=True, default=2) null_boolean_field = models.BooleanField(null=True, default=False) null_boolean_field_old = models.NullBooleanField(null=True, default=False) positive_integer_field = models.PositiveIntegerField(null=True, default=3) positive_small_integer_field = models.PositiveSmallIntegerField(null=True, default=4) small_integer_field = models.SmallIntegerField(null=True, default=5) time_field = models.TimeField(null=True, default=timezone.now) # Fields not required in BulkInsertMapper char_field = models.CharField(null=True, max_length=4, default='char') email_field = models.EmailField(null=True, default='*****@*****.**') file_field = models.FileField(null=True, default='file.txt') file_path_field = models.FilePathField(path='/tmp', null=True, default='file.txt') generic_ip_address_field = models.GenericIPAddressField(null=True, default='127.0.0.1') if Image: image_field = models.ImageField(null=True, default='image.jpg') slug_field = models.SlugField(null=True, default='slug') text_field = models.TextField(null=True, default='text') url_field = models.URLField(null=True, default='/') uuid_field = models.UUIDField(null=True, default=uuid.uuid4)
class Item(models.Model): name = models.CharField(max_length=15) text = models.TextField(default="xyzzy") value = models.IntegerField() other_value = models.IntegerField(default=0) def __str__(self): return self.name
class Person(models.Model): first_name = models.CharField(max_length=100, unique=True) last_name = models.CharField(max_length=100) birthday = models.DateField() defaults = models.TextField() def __str__(self): return '%s %s' % (self.first_name, self.last_name)
class Comment(models.Model): article = models.ForeignKey(Article, models.CASCADE, related_name="comments") text = models.TextField() pub_date = models.DateTimeField() approval_date = models.DateTimeField(null=True) def __str__(self): return 'Comment to %s (%s)' % (self.article.title, self.pub_date)
class Line(PostgreSQLModel): scene = models.ForeignKey('Scene', models.CASCADE) character = models.ForeignKey('Character', models.CASCADE) dialogue = models.TextField(blank=True, null=True) dialogue_search_vector = SearchVectorField(blank=True, null=True) dialogue_config = models.CharField(max_length=100, blank=True, null=True) def __str__(self): return self.dialogue or ''
class AuthorAddress(models.Model): author = models.ForeignKey(Author, models.CASCADE, to_field='name', related_name='addresses') address = models.TextField() class Meta: ordering = ['id'] def __str__(self): return self.address
class ArticleTranslation(models.Model): article = models.ForeignKey('indexes.Article', models.CASCADE) article_no_constraint = models.ForeignKey('indexes.Article', models.CASCADE, db_constraint=False, related_name='+') language = models.CharField(max_length=10, unique=True) content = models.TextField()
class Category(models.Model): title = models.CharField(max_length=100) description = models.TextField() def __str__(self): return self.title class Meta: ordering = ('title',)
class Article(models.Model): authors = models.ManyToManyField(Author, related_name='articles') title = models.CharField(max_length=50) summary = models.CharField(max_length=200, null=True, blank=True) text = models.TextField() written = models.DateTimeField() published = models.DateTimeField(null=True, blank=True) updated = models.DateTimeField(null=True, blank=True) views = models.PositiveIntegerField(default=0) def __str__(self): return self.title
class Migration(migrations.Migration): dependencies = [ ('sites', '0001_initial'), ] operations = [ migrations.CreateModel( name='FlatPage', fields=[ ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('url', models.CharField(max_length=100, verbose_name='URL', db_index=True)), ('title', models.CharField(max_length=200, verbose_name='title')), ('content', models.TextField(verbose_name='content', blank=True)), ('enable_comments', models.BooleanField(default=False, verbose_name='enable comments')), ('template_name', models.CharField(help_text=( "Example: 'flatpages/contact_page.html'. If this isn't provided, the system will use " "'flatpages/default.html'."), max_length=70, verbose_name='template name', blank=True)), ('registration_required', models.BooleanField( default=False, help_text= 'If this is checked, only logged-in users will be able to view the page.', verbose_name='registration required')), ('sites', models.ManyToManyField(to='sites.Site', verbose_name='sites')), ], options={ 'ordering': ('url', ), 'db_table': 'django_flatpage', 'verbose_name': 'flat page', 'verbose_name_plural': 'flat pages', }, bases=(models.Model, ), ), ]
def test_create_model_no_reordering_for_unrelated_fk(self): """ CreateModel order remains unchanged if the later AddField operation isn't a FK between them. """ self.assertDoesNotOptimize([ migrations.CreateModel( 'Foo', [('name', models.CharField(max_length=255))]), migrations.CreateModel('Link', [('url', models.TextField())]), migrations.AddField( 'Other', 'link', models.ForeignKey('migrations.Link', models.CASCADE)), ], )
class Attachment(models.Model): post = models.ForeignKey( Post, models.CASCADE, related_name='attached_%(class)s_set', related_query_name='attached_%(app_label)s_%(class)ss', ) content = models.TextField() class Meta: abstract = True def __str__(self): return self.content
def test_create_model_reordering(self): """ AddField optimizes into CreateModel if it's a FK to a model that's between them (and there's no FK in the other direction), by changing the order of the CreateModel operations. """ self.assertOptimizesTo( [ migrations.CreateModel( 'Foo', [('name', models.CharField(max_length=255))]), migrations.CreateModel('Link', [('url', models.TextField())]), migrations.AddField( 'Foo', 'link', models.ForeignKey('migrations.Link', models.CASCADE)), ], [ migrations.CreateModel('Link', [('url', models.TextField())]), migrations.CreateModel('Foo', [ ('name', models.CharField(max_length=255)), ('link', models.ForeignKey('migrations.Link', models.CASCADE)) ]), ], )
class Donut(models.Model): name = models.CharField(max_length=100) is_frosted = models.BooleanField(default=False) has_sprinkles = models.BooleanField(null=True) has_sprinkles_old = models.NullBooleanField() baked_date = models.DateField(null=True) baked_time = models.TimeField(null=True) consumed_at = models.DateTimeField(null=True) review = models.TextField() class Meta: ordering = ('consumed_at', ) def __str__(self): return self.name
class Article(models.Model): headline = models.CharField(max_length=100) pub_date = models.DateField() body = models.TextField(default='') reporter = models.ForeignKey(Reporter, models.CASCADE) response_to = models.ForeignKey('self', models.SET_NULL, null=True) unmanaged_reporters = models.ManyToManyField(Reporter, through='ArticleReporter', related_name='+') def __str__(self): return self.headline class Meta: ordering = ('headline', ) index_together = [ ["headline", "pub_date"], ['headline', 'response_to', 'pub_date', 'reporter'], ]
class Article(models.Model): headline = models.CharField(max_length=50) slug = models.SlugField() pub_date = models.DateField() created = models.DateField(editable=False) writer = models.ForeignKey(Writer, models.CASCADE) article = models.TextField() categories = models.ManyToManyField(Category, blank=True) status = models.PositiveIntegerField(choices=ARTICLE_STATUS, blank=True, null=True) def save(self, *args, **kwargs): if not self.id: self.created = datetime.date.today() return super().save(*args, **kwargs) def __str__(self): return self.headline
class AllFieldsModel(models.Model): big_integer = models.BigIntegerField() binary = models.BinaryField() boolean = models.BooleanField(default=False) char = models.CharField(max_length=10) date = models.DateField() datetime = models.DateTimeField() decimal = models.DecimalField(decimal_places=2, max_digits=2) duration = models.DurationField() email = models.EmailField() file_path = models.FilePathField() floatf = models.FloatField() integer = models.IntegerField() generic_ip = models.GenericIPAddressField() null_boolean = models.NullBooleanField() positive_integer = models.PositiveIntegerField() positive_small_integer = models.PositiveSmallIntegerField() slug = models.SlugField() small_integer = models.SmallIntegerField() text = models.TextField() time = models.TimeField() url = models.URLField() uuid = models.UUIDField() fo = ForeignObject('self', on_delete=models.CASCADE, from_fields=['abstract_non_concrete_id'], to_fields=['id'], related_name='reverse') fk = ForeignKey('self', models.CASCADE, related_name='reverse2') m2m = ManyToManyField('self') oto = OneToOneField('self', models.CASCADE) object_id = models.PositiveIntegerField() content_type = models.ForeignKey(ContentType, models.CASCADE) gfk = GenericForeignKey() gr = GenericRelation(DataModel)
class CaseTestModel(models.Model): integer = models.IntegerField() integer2 = models.IntegerField(null=True) string = models.CharField(max_length=100, default='') big_integer = models.BigIntegerField(null=True) binary = models.BinaryField(default=b'') boolean = models.BooleanField(default=False) date = models.DateField(null=True, db_column='date_field') date_time = models.DateTimeField(null=True) decimal = models.DecimalField(max_digits=2, decimal_places=1, null=True, db_column='decimal_field') duration = models.DurationField(null=True) email = models.EmailField(default='') file = models.FileField(null=True, db_column='file_field') file_path = models.FilePathField(null=True) float = models.FloatField(null=True, db_column='float_field') if Image: image = models.ImageField(null=True) generic_ip_address = models.GenericIPAddressField(null=True) null_boolean = models.BooleanField(null=True) null_boolean_old = models.NullBooleanField() positive_integer = models.PositiveIntegerField(null=True) positive_small_integer = models.PositiveSmallIntegerField(null=True) slug = models.SlugField(default='') small_integer = models.SmallIntegerField(null=True) text = models.TextField(default='') time = models.TimeField(null=True, db_column='time_field') url = models.URLField(default='') uuid = models.UUIDField(null=True) fk = models.ForeignKey('self', models.CASCADE, null=True) def __str__(self): return "%i, %s" % (self.integer, self.string)
class ColumnTypes(models.Model): id = models.AutoField(primary_key=True) big_int_field = models.BigIntegerField() bool_field = models.BooleanField(default=False) null_bool_field = models.BooleanField(null=True) char_field = models.CharField(max_length=10) null_char_field = models.CharField(max_length=10, blank=True, null=True) date_field = models.DateField() date_time_field = models.DateTimeField() decimal_field = models.DecimalField(max_digits=6, decimal_places=1) email_field = models.EmailField() file_field = models.FileField(upload_to="unused") file_path_field = models.FilePathField() float_field = models.FloatField() int_field = models.IntegerField() gen_ip_address_field = models.GenericIPAddressField(protocol="ipv4") pos_int_field = models.PositiveIntegerField() pos_small_int_field = models.PositiveSmallIntegerField() slug_field = models.SlugField() small_int_field = models.SmallIntegerField() text_field = models.TextField() time_field = models.TimeField() url_field = models.URLField() uuid_field = models.UUIDField()
class SystemDetails(models.Model): details = models.TextField()