django表列读取无错误

时间:2016-11-25 10:18:22

标签: python mysql django

使用django版本1.10.3我正在创建mysql表

MariaDB [ipp_database]> SELECT * FROM polls_question;
+----+----------------+----------------------------+
| id | question_text  | pub_date                   |
+----+----------------+----------------------------+
|  1 | What's up?     | 2016-10-06 11:04:36.703335 |
|  2 | Klappt das so? | 2016-11-24 09:39:11.953693 |
|  3 | Klappt das so? | 2016-11-24 09:40:08.260329 |
|  4 | Klappt das so? | 2016-11-24 10:32:54.000000 |
|  5 | Klappt das so? | 2016-11-24 10:32:57.000000 |
+----+----------------+----------------------------+
5 rows in set (0.00 sec)

使用模型

from __future__ import unicode_literals

from django.db import models

# Create your models here.


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text


class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text

和命令

from django.core.management.base import BaseCommand
from polls.models import Question
from django.utils import timezone


class Command(BaseCommand):
    def handle(self, *args, **kwargs):
        q = Question(question_text="Klappt das so?", pub_date=timezone.now())
        q.save()
        for question in Question.objects.order_by('id'):
            print u"%s %s %s" % (question.id, question.question_text, question.pub_date)

在mysql终端中它显示正确,但是当使用发布的命令进行阅读时,它会给出

1 What's up? None
2 Klappt das so? None
3 Klappt das so? None
4 Klappt das so? None
5 Klappt das so? None

任何想法,为什么“pub_date”列读取None? 谢谢, 丹尼尔

1 个答案:

答案 0 :(得分:3)

这是一个已知问题https://code.djangoproject.com/ticket/19716

ALTER TABLE polls_question MODIFY COLUMN pub_date datetime NOT NULL