为什么这不是DateTimeField的有效条目?

时间:2016-01-20 19:35:56

标签: django csv django-models django-admin django-import-export

我尝试使用django-import-export导入csv文件但遇到了这个荒谬的错误:

Line number: 44 - Column 'completion_date': Enter a valid date.
OrderedDict([(u'id', u''), ('language', 1868), ('type', 10), (u'language_code', u'fr'), ('completion_date', datetime.date(2015, 12, 31)), (u'document_slug', u'obs'), (u'is_online', u'1'), (u'completion_rate', u'50'), (u'is_done', u'0'), (u'completion_date_string', u'2015-12-31'), (u'in_door43', u'1'), (u'in_uw', u'1')])
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/import_export/resources.py", line 374, in import_data
self.import_obj(instance, row, real_dry_run)
File "/usr/local/lib/python2.7/dist-packages/import_export/resources.py", line 238, in import_obj
self.import_field(field, obj, data)
ValueError: Column 'completion_date': Enter a valid date.

这是我的课程,将completion_date定义为DateField

python_2_unicode_compatible
class Progress(models.Model):

    language = models.ForeignKey('td.Language', limit_choices_to={'gateway_flag': True})
    type = models.ForeignKey('Document')
    is_online = models.NullBooleanField()
    method = models.CharField(max_length=200, choices=METHODS, blank=True)
    completion_rate = models.PositiveSmallIntegerField(choices=COMPLETION_RATE, null=True, blank=True)
    completion_date = models.DateField(null=True, blank=True)
    qa_level = models.PositiveSmallIntegerField(choices=QA_LEVEL, null=True, blank=True)
    in_door43 = models.NullBooleanField()
    in_uw = models.NullBooleanField()
    partners = models.ManyToManyField('Partner', blank=True)
    notes = models.TextField(blank=True)
    is_done = models.BooleanField(default="false")

    def __str__(self):
        return str(self.type)

    class Meta:
        unique_together = ("language", "type")

为什么datetime.date()不是有效日期?

2 个答案:

答案 0 :(得分:0)

似乎django的DateField接受字符串作为默认值。 尝试转换您的日期时间字段,如下所示:

YourModel.completion_date = your_csv_dict['completion_date'].strftime("%Y-%d-%m")

答案 1 :(得分:0)

嗯,看起来进口商期待一个日期为字符串。当我不将字符串转换为datetime.date()时,它可以正常工作。我的日期字符串采用yyyy-mm-dd格式(如completion_date_string列所示)。