0

我正在尝试使用导入 csv 文件,django-import-export但遇到了这个无意义的错误:

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_dateDateField

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()不是有效日期?

4

2 回答 2

0

django 似乎DateField默认接受字符串。尝试像这样转换您的日期时间字段:

YourModel.completion_date = your_csv_dict['completion_date'].strftime("%Y-%d-%m")
于 2016-01-20T20:13:45.860 回答
0

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

于 2016-01-20T20:14:37.987 回答