Example #1
0
class Migration(migrations.Migration):

    initial = True

    operations = [
        migrations.CreateModel(
            "Author",
            [
                ("id", models.AutoField(primary_key=True)),
                ("name", models.CharField(max_length=255)),
                ("slug", models.SlugField(null=True)),
                ("age", models.IntegerField(default=0)),
                ("silly_field", models.BooleanField(default=False)),
            ],
        ),
        migrations.CreateModel(
            "Tribble",
            [
                ("id", models.AutoField(primary_key=True)),
                ("fluffy", models.BooleanField(default=True)),
            ],
        ),
        migrations.AlterUniqueTogether(
            name='author',
            unique_together={('name', 'slug')},
        ),
    ]
Example #2
0
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)
Example #3
0
class Restaurant(models.Model):
    place = models.OneToOneField(Place, models.CASCADE, primary_key=True)
    serves_hot_dogs = models.BooleanField(default=False)
    serves_pizza = models.BooleanField(default=False)

    def __str__(self):
        return "%s the restaurant" % self.place.name
Example #4
0
class Restaurant(Place, Rating):
    serves_hot_dogs = models.BooleanField(default=False)
    serves_pizza = models.BooleanField(default=False)
    chef = models.ForeignKey(Chef, models.SET_NULL, null=True, blank=True)

    class Meta(Rating.Meta):
        db_table = 'my_restaurant'

    def __str__(self):
        return "%s the restaurant" % self.name
Example #5
0
 def test_boolean_field(self):
     field = models.BooleanField()
     name, path, args, kwargs = field.deconstruct()
     self.assertEqual(path, "djmodels.db.models.BooleanField")
     self.assertEqual(args, [])
     self.assertEqual(kwargs, {})
     field = models.BooleanField(default=True)
     name, path, args, kwargs = field.deconstruct()
     self.assertEqual(path, "djmodels.db.models.BooleanField")
     self.assertEqual(args, [])
     self.assertEqual(kwargs, {"default": True})
Example #6
0
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, ),
        ),
    ]
Example #7
0
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
Example #8
0
class Person(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    fun = models.BooleanField(default=False)

    favorite_book = models.ForeignKey('Book',
                                      models.SET_NULL,
                                      null=True,
                                      related_name='favorite_books')
    favorite_thing_type = models.ForeignKey('contenttypes.ContentType',
                                            models.SET_NULL,
                                            null=True)
    favorite_thing_id = models.IntegerField(null=True)
    favorite_thing = GenericForeignKey('favorite_thing_type',
                                       'favorite_thing_id')

    objects = PersonManager()
    fun_people = FunPeopleManager()
    boring_people = BoringPeopleManager()

    custom_queryset_default_manager = CustomQuerySet.as_manager()
    custom_queryset_custom_manager = CustomManager('hello')
    custom_init_queryset_manager = CustomInitQuerySet.as_manager()

    def __str__(self):
        return "%s %s" % (self.first_name, self.last_name)
Example #9
0
class Book(models.Model):
    title = models.CharField(max_length=50)
    author = models.CharField(max_length=30)
    is_published = models.BooleanField(default=False)
    authors = models.ManyToManyField(Person, related_name='books')
    fun_authors = models.ManyToManyField(FunPerson, related_name='books')
    favorite_things = GenericRelation(
        Person,
        content_type_field='favorite_thing_type',
        object_id_field='favorite_thing_id',
    )
    fun_people_favorite_things = GenericRelation(
        FunPerson,
        content_type_field='favorite_thing_type',
        object_id_field='favorite_thing_id',
    )

    published_objects = PublishedBookManager()
    annotated_objects = AnnotatedBookManager()

    class Meta:
        base_manager_name = 'annotated_objects'

    def __str__(self):
        return self.title
Example #10
0
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")
Example #11
0
class AggregateTestModel(models.Model):
    """
    To test postgres-specific general aggregation functions
    """
    char_field = models.CharField(max_length=30, blank=True)
    integer_field = models.IntegerField(null=True)
    boolean_field = models.BooleanField(null=True)
Example #12
0
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)
Example #13
0
class Vegetable(models.Model):
    name = models.CharField(max_length=150)
    is_yucky = models.BooleanField(default=True)

    tags = GenericRelation(TaggedItem)

    def __str__(self):
        return self.name
Example #14
0
 def test_booleanfield_choices_blank(self):
     """
     BooleanField with choices and defaults doesn't generate a formfield
     with the blank option (#9640, #10549).
     """
     choices = [(1, 'Si'), (2, 'No')]
     f = models.BooleanField(choices=choices, default=1, null=False)
     self.assertEqual(f.formfield().choices, choices)
Example #15
0
class RelatedModel(models.Model):
    test_gfk = GenericRelation('RelationModel',
                               content_type_field='gfk_ctype',
                               object_id_field='gfk_id')
    exact = models.BooleanField(null=True)

    def __str__(self):
        return str(self.pk)
Example #16
0
class OtherAuthor(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    slug = models.SlugField(null=True)
    age = models.IntegerField(default=0)
    silly_field = models.BooleanField(default=False)

    class Meta:
        app_label = "migrated_unapplied_app"
Example #17
0
class ManagedModel(models.Model):
    data = models.CharField(max_length=10)
    tag = models.ForeignKey(Tag, models.CASCADE)
    public = models.BooleanField(default=True)

    objects = CustomManager()
    normal_manager = models.Manager()

    def __str__(self):
        return self.data
Example #18
0
class OneToOneRestrictedModel(models.Model):
    name = models.CharField(max_length=50)
    is_public = models.BooleanField(default=False)
    related = models.OneToOneField(RelatedModel, models.CASCADE)

    objects = RestrictedManager()
    plain_manager = models.Manager()

    def __str__(self):
        return self.name
Example #19
0
class Migration(migrations.Migration):

    operations = [
        migrations.CreateModel(
            "Author",
            [
                ("id", models.AutoField(primary_key=True)),
                ("name", models.CharField(max_length=255)),
                ("slug", models.SlugField(null=True)),
                ("age", models.IntegerField(default=0)),
                ("silly_field", models.BooleanField(default=False)),
            ],
        ),
        migrations.CreateModel(
            "Tribble",
            [
                ("id", models.AutoField(primary_key=True)),
                ("fluffy", models.BooleanField(default=True)),
            ],
        )
    ]
Example #20
0
class Migration(migrations.Migration):

    operations = [
        migrations.CreateModel(
            "Salamander",
            [
                ("id", models.AutoField(primary_key=True)),
                ("size", models.IntegerField(default=0)),
                ("silly_field", models.BooleanField(default=False)),
            ],
        ),
    ]
Example #21
0
class Employee(models.Model):
    # The order of these fields matter, do not change. Certain backends
    # rely on field ordering to perform database conversions, and this
    # model helps to test that.
    first_name = models.CharField(max_length=20)
    manager = models.BooleanField(default=False)
    last_name = models.CharField(max_length=20)
    store = models.ForeignKey(Store, models.CASCADE)
    age = models.IntegerField()
    salary = models.DecimalField(max_digits=8, decimal_places=2)

    def __str__(self):
        return '%s %s' % (self.first_name, self.last_name)
Example #22
0
class PublicationDefaults(models.Model):
    MODE_CHOICES = (('di', 'direct'), ('de', 'delayed'))
    CATEGORY_CHOICES = ((1, 'Games'), (2, 'Comics'), (3, 'Novel'))
    title = models.CharField(max_length=30)
    date_published = models.DateField(default=datetime.date.today)
    datetime_published = models.DateTimeField(
        default=datetime.datetime(2000, 1, 1))
    mode = models.CharField(max_length=2,
                            choices=MODE_CHOICES,
                            default=default_mode)
    category = models.IntegerField(choices=CATEGORY_CHOICES,
                                   default=default_category)
    active = models.BooleanField(default=True)
    file = models.FileField(default='default.txt')
Example #23
0
class Migration(migrations.Migration):

    initial = True

    operations = [
        migrations.CreateModel(
            'Salamander',
            [
                ('id', models.AutoField(primary_key=True)),
                ('tail', models.IntegerField(default=0)),
                ('silly_field', models.BooleanField(default=False)),
            ],
        ),
        migrations.RunPython(grow_tail, shrink_tail),
    ]
Example #24
0
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)
Example #25
0
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()
Example #26
0
class Migration(migrations.Migration):

    dependencies = []

    operations = [
        migrations.CreateModel(
            "OtherAuthor",
            [
                ("id", models.AutoField(primary_key=True)),
                ("name", models.CharField(max_length=255)),
                ("slug", models.SlugField(null=True)),
                ("age", models.IntegerField(default=0)),
                ("silly_field", models.BooleanField(default=False)),
            ],
        ),
    ]
Example #27
0
class Migration(migrations.Migration):

    dependencies = []

    operations = [
        migrations.CreateModel(
            name='SillyModel',
            fields=[
                ('id',
                 models.AutoField(verbose_name='ID',
                                  serialize=False,
                                  auto_created=True,
                                  primary_key=True)),
                ('silly_field', models.BooleanField(default=False)),
            ],
            options={},
            bases=(models.Model, ),
        ),
    ]
Example #28
0
class FunPerson(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    fun = models.BooleanField(default=True)
    favorite_book = models.ForeignKey(
        'Book',
        models.SET_NULL,
        null=True,
        related_name='fun_people_favorite_books',
    )
    favorite_thing_type = models.ForeignKey('contenttypes.ContentType',
                                            models.SET_NULL,
                                            null=True)
    favorite_thing_id = models.IntegerField(null=True)
    favorite_thing = GenericForeignKey('favorite_thing_type',
                                       'favorite_thing_id')

    objects = FunPeopleManager()

    def __str__(self):
        return "%s %s" % (self.first_name, self.last_name)
Example #29
0
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)
Example #30
0
class ItalianRestaurant(Restaurant):
    serves_gnocchi = models.BooleanField(default=False)

    def __str__(self):
        return "%s the italian restaurant" % self.name