Issue
I have the following model:
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))
However, when I crate tables, table messages
looks like this:
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)
Notice, that status
field doesn't have a default value.
The same case applies for a few other tables and fields.
What am I doing wrong?
Solution
Nothing. Django defaults are applied in code, not in the database.
Answered By - Daniel Roseman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.