Exemplo n.º 1
0
class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    expire_date = models.DateField()

    class Meta:
        get_latest_by = 'pub_date'
Exemplo n.º 2
0
class Comment(models.Model):
    article = models.ForeignKey(Article,
                                models.CASCADE,
                                related_name="comments")
    text = models.TextField()
    pub_date = models.DateField()
    approval_date = models.DateField(null=True)

    def __str__(self):
        return 'Comment to %s (%s)' % (self.article.title, self.pub_date)
Exemplo n.º 3
0
 def test_date_field(self):
     field = models.DateField()
     name, path, args, kwargs = field.deconstruct()
     self.assertEqual(path, "djmodels.db.models.DateField")
     self.assertEqual(args, [])
     self.assertEqual(kwargs, {})
     field = models.DateField(auto_now=True)
     name, path, args, kwargs = field.deconstruct()
     self.assertEqual(path, "djmodels.db.models.DateField")
     self.assertEqual(args, [])
     self.assertEqual(kwargs, {"auto_now": True})
Exemplo n.º 4
0
class DTModel(models.Model):
    name = models.CharField(max_length=32)
    start_datetime = models.DateTimeField(null=True, blank=True)
    end_datetime = models.DateTimeField(null=True, blank=True)
    start_date = models.DateField(null=True, blank=True)
    end_date = models.DateField(null=True, blank=True)
    start_time = models.TimeField(null=True, blank=True)
    end_time = models.TimeField(null=True, blank=True)
    duration = models.DurationField(null=True, blank=True)

    def __str__(self):
        return 'DTModel({0})'.format(self.name)
Exemplo n.º 5
0
class Experiment(models.Model):
    name = models.CharField(max_length=24)
    assigned = models.DateField()
    completed = models.DateField()
    estimated_time = models.DurationField()
    start = models.DateTimeField()
    end = models.DateTimeField()

    class Meta:
        db_table = 'expressions_ExPeRiMeNt'
        ordering = ('name',)

    def duration(self):
        return self.end - self.start
Exemplo n.º 6
0
class RangeLookupsModel(PostgreSQLModel):
    parent = models.ForeignKey(RangesModel, models.SET_NULL, blank=True, null=True)
    integer = models.IntegerField(blank=True, null=True)
    big_integer = models.BigIntegerField(blank=True, null=True)
    float = models.FloatField(blank=True, null=True)
    timestamp = models.DateTimeField(blank=True, null=True)
    date = models.DateField(blank=True, null=True)
Exemplo n.º 7
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)
Exemplo n.º 8
0
class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()

    def __str__(self):
        return self.headline

    def was_published_today(self):
        return self.pub_date == datetime.date.today()

    def articles_from_same_day_1(self):
        return Article.objects.filter(pub_date=self.pub_date).exclude(
            id=self.id)

    def articles_from_same_day_2(self):
        """
        Verbose version of get_articles_from_same_day_1, which does a custom
        database query for the sake of demonstration.
        """
        from djmodels.db import connection
        with connection.cursor() as cursor:
            cursor.execute(
                """
                SELECT id, headline, pub_date
                FROM custom_methods_article
                WHERE pub_date = %s
                    AND id != %s""",
                [connection.ops.adapt_datefield_value(self.pub_date), self.id])
            return [self.__class__(*row) for row in cursor.fetchall()]
Exemplo n.º 9
0
class UniqueForDateModel(models.Model):
    start_date = models.DateField()
    end_date = models.DateTimeField()
    count = models.IntegerField(unique_for_date="start_date",
                                unique_for_year="end_date")
    order = models.IntegerField(unique_for_month="end_date")
    name = models.CharField(max_length=100)
Exemplo n.º 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")
Exemplo n.º 11
0
class AuthorMeeting(models.Model):
    name = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    created = models.DateField(editable=False)

    def __str__(self):
        return self.name
Exemplo n.º 12
0
 def process_rhs(self, compiler, connection):
     # Transform rhs value for db lookup.
     if isinstance(self.rhs, datetime.date):
         output_field = models.DateTimeField() if isinstance(
             self.rhs, datetime.datetime) else models.DateField()
         value = models.Value(self.rhs, output_field=output_field)
         self.rhs = value.resolve_expression(compiler.query)
     return super().process_rhs(compiler, connection)
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
class Item(models.Model):
    name = models.CharField(max_length=30)
    date = models.DateField()
    time = models.TimeField()
    last_modified = models.DateTimeField()

    def __str__(self):
        return self.name
Exemplo n.º 15
0
class Article(models.Model):
    title = models.CharField(max_length=100)
    pub_date = models.DateTimeField()
    published_on = models.DateField(null=True)

    categories = models.ManyToManyField("Category", related_name="articles")

    def __str__(self):
        return self.title
Exemplo n.º 16
0
class Article(models.Model):
    title = models.CharField(max_length=100)
    pub_date = models.DateField()
    pub_datetime = models.DateTimeField(default=timezone.now())

    categories = models.ManyToManyField("Category", related_name="articles")

    def __str__(self):
        return self.title
Exemplo n.º 17
0
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
Exemplo n.º 18
0
class Employee(models.Model):
    name = models.CharField(max_length=40, blank=False, null=False)
    salary = models.PositiveIntegerField()
    department = models.CharField(max_length=40, blank=False, null=False)
    hire_date = models.DateField(blank=False, null=False)
    age = models.IntegerField(blank=False, null=False)

    def __str__(self):
        return '{}, {}, {}, {}'.format(self.name, self.department, self.salary,
                                       self.hire_date)
Exemplo n.º 19
0
class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    reporter = models.ForeignKey(Reporter, models.CASCADE)

    def __str__(self):
        return self.headline

    class Meta:
        ordering = ('headline', )
Exemplo n.º 20
0
class FlexibleDatePost(models.Model):
    title = models.CharField(max_length=50,
                             unique_for_date='posted',
                             blank=True)
    slug = models.CharField(max_length=50,
                            unique_for_year='posted',
                            blank=True)
    subtitle = models.CharField(max_length=50,
                                unique_for_month='posted',
                                blank=True)
    posted = models.DateField(blank=True, null=True)
Exemplo n.º 21
0
class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    reporter = models.ForeignKey(Reporter, models.CASCADE)
    reporter_proxy = models.ForeignKey(
        ReporterProxy,
        models.SET_NULL,
        null=True,
        related_name='reporter_proxy',
    )

    def __str__(self):
        return self.headline
Exemplo n.º 22
0
class Author(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    dob = models.DateField()

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Protect against annotations being passed to __init__ --
        # this'll make the test suite get angry if annotations aren't
        # treated differently than fields.
        for k in kwargs:
            assert k in [f.attname for f in self._meta.fields], \
                "Author.__init__ got an unexpected parameter: %s" % k
Exemplo n.º 23
0
class Post(models.Model):
    title = models.CharField(max_length=50,
                             unique_for_date='posted',
                             blank=True)
    slug = models.CharField(max_length=50,
                            unique_for_year='posted',
                            blank=True)
    subtitle = models.CharField(max_length=50,
                                unique_for_month='posted',
                                blank=True)
    posted = models.DateField()

    def __str__(self):
        return self.title
Exemplo n.º 24
0
class Book(models.Model):
    objects = BookManager()
    title = models.CharField(max_length=100)
    published = models.DateField()
    authors = models.ManyToManyField(Person)
    editor = models.ForeignKey(Person, models.SET_NULL, null=True, related_name='edited')
    reviews = GenericRelation(Review)
    pages = models.IntegerField(default=100)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ('title',)
Exemplo n.º 25
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')
Exemplo n.º 26
0
 def test_date_field_raises_error_message(self):
     f = models.DateField()
     self._test_validation_messages(f, 'fõo', [
         "'fõo' value has an invalid date format. It must be in YYYY-MM-DD format."
     ])
     self._test_validation_messages(f, 'aaaa-10-10', [
         "'aaaa-10-10' value has an invalid date format. It must be in YYYY-MM-DD format."
     ])
     self._test_validation_messages(f, '2011-13-10', [
         "'2011-13-10' value has the correct format (YYYY-MM-DD) but it is an invalid date."
     ])
     self._test_validation_messages(f, '2011-10-32', [
         "'2011-10-32' value has the correct format (YYYY-MM-DD) but it is an invalid date."
     ])
Exemplo n.º 27
0
class Book(models.Model):
    isbn = models.CharField(max_length=9)
    name = models.CharField(max_length=255)
    pages = models.IntegerField()
    rating = models.FloatField()
    price = models.DecimalField(decimal_places=2, max_digits=6)
    authors = models.ManyToManyField(Author)
    contact = models.ForeignKey(Author,
                                models.CASCADE,
                                related_name='book_contact_set')
    publisher = models.ForeignKey(Publisher, models.CASCADE)
    pubdate = models.DateField()

    def __str__(self):
        return self.name
Exemplo n.º 28
0
class Thing(models.Model):
    when = models.CharField(max_length=1, primary_key=True)
    join = models.CharField(max_length=1)
    like = models.CharField(max_length=1)
    drop = models.CharField(max_length=1)
    alter = models.CharField(max_length=1)
    having = models.CharField(max_length=1)
    where = models.DateField(max_length=1)
    has_hyphen = models.CharField(max_length=1, db_column='has-hyphen')

    class Meta:
        db_table = 'select'

    def __str__(self):
        return self.when
Exemplo n.º 29
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
Exemplo n.º 30
0
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'],
        ]