Django没有应用默认值= 0

时间:2014-04-18 18:48:13

标签: python django

我有以下型号:

class Message(models.Model):
    MS_DRAFT, MS_NEW, MS_WAIT, MS_SENT, MS_DELETED, MS_GEOMSG, MS_ERROR  = range(7)
    MS_CHOICES = (
            (MS_DRAFT, "Draft"),
            (MS_NEW, "New"),
            (MS_SENT, "Sent"),
            (MS_DELETED, "Deleted"),
            (MS_ERROR, "Error"),
            (MS_GEOMSG, "Geo-msg"),
        )

    user = models.ForeignKey(User)
    sender = models.CharField(blank=False, null=False, max_length=255)
    subject = models.CharField(blank=False, null=False, max_length=255)
    headers = models.CharField(null=True, blank=True, default='', max_length=255)
    created = models.DateTimeField(default=datetime.utcnow().replace(tzinfo=utc))
    updated = models.DateTimeField(default=datetime.utcnow().replace(tzinfo=utc))
    planned = models.DateTimeField(default=datetime.utcnow().replace(tzinfo=utc))
    expires = models.DateTimeField(default=plus_one_month)
    status = models.IntegerField(choices=MS_CHOICES, default=MS_DRAFT)
    locale = models.CharField(max_length=255)

    def mark_deleted(self):
        self.status = MS_DELETED
        self.save()

    def save(self, *args, **kwargs):
        self.updated = datetime.utcnow().replace(tzinfo=utc)
        super(Message, self).save(*args, **kwargs)

    class Meta:
        db_table = 'messages'

    def __unicode__(self):
        return unicode('{0}:{1}:{2}:{3}:{4}'.format(self.sender, self.subject, self.locale, self.planned, self.expires))

但是,当我创建表时,表messages看起来像这样:

mysql> desc messages;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id | int(11)      | NO   |     | NULL    |                |
| sender  | varchar(255) | NO   |     | NULL    |                |
| subject | varchar(255) | NO   |     | NULL    |                |
| headers | varchar(255) | YES  |     | NULL    |                |
| created | datetime     | NO   |     | NULL    |                |
| updated | datetime     | NO   |     | NULL    |                |
| planned | datetime     | NO   |     | NULL    |                |
| expires | datetime     | NO   |     | NULL    |                |
| status  | int(11)      | NO   |     | NULL    |                |
| locale  | varchar(255) | NO   |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)

请注意,status字段没有默认值。 同样的情况适用于其他一些表和字段。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

无。 Django默认值应用于代码中,而不是数据库中。