Django List Comprehension - Trouble Comparing Datetime Objects. TypeError: unorderable types: datetime.date() <= str()
Django List Comprehension - Trouble Comparing Datetime Objects. TypeError: unorderable types: datetime.date() <= str()
在我的模型中,我有以下方法:
def _bags_remaining(self):
current_set = SortingRecords.objects.values().filter(~Q(id=self.id), tag=self.tag)
sorted = [SortingRecords['bags_sorted'] for SortingRecords in current_set if
SortingRecords['date'] <= self.date]
remaining = self.tag.pieces - sum(sorted) - self.bags_sorted
return remaining
bags_remaining = property(_bags_remaining)
它旨在查找到目前为止在与记录关联的标签下分类的行李数量,并从行李总数中扣除该数量(连同在该记录下分类的数量)。
效果很好!适当的金额已成功传递到模板。
然而,令我失望的是它取消了我的单元测试。
======================================================================
ERROR: test_sorting_records_bags_remaining_calculation (AlmondKing.InventoryLogs.tests.test_views.test_purchase_details.DetailsPageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\tests\test_views\test_purchase_details.py", line 155, in test_sorting_records_bags_remaining_calculation
self.assertEqual(self.sortrecord1.bags_remaining, 79)
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\models.py", line 115, in _bags_remaining
sorted = [SortingRecords['bags_sorted'] for SortingRecords in current_set if
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\models.py", line 116, in <listcomp>
SortingRecords['date'] <= self.date]
TypeError: unorderable types: datetime.date() <= str()
----------------------------------------------------------------------
Ran 22 tests in 0.203s
FAILED (errors=2)
Destroying test database for alias 'default'...
它似乎将我的日期对象解释为字符串。它从中提取的模型是 DateField。如果我在上面调用类型,它会报告为:
这是它所在的型号:
class SortingRecords(models.Model):
tag = models.ForeignKey(Purchase, related_name='sorting_record')
date = models.DateField()
bags_sorted = models.IntegerField()
turnout = models.IntegerField()
objects = models.Manager()
def __str__(self):
return "%s [%s]" % (self.date, self.tag.tag)
这是我的考试运行。
# Sorting Records should calculate bags remaining for each entry.
def test_sorting_records_bags_remaining_calculation(self):
self.assertEqual(self.sortrecord1.bags_remaining, 79)
self.assertEqual(self.sortrecord2.bags_remaining, 39)
self.assertEqual(self.sortrecord3.bags_remaining, 9)
同样,它在现实生活中有效,但在 运行 测试中失败了。有什么想法吗?
编辑以添加详细信息:
使用的数据库是 Postgres。
这是我的测试 setUpTestData():
class DetailsPageTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.product1 = ProductGroup.objects.create(
product_name="Almonds"
)
cls.variety1 = Variety.objects.create(
product_group = cls.product1,
variety_name = "non pareil",
husked = False,
finished = False,
)
cls.supplier1 = Supplier.objects.create(
company_name = "Acme",
company_location = "Acme Acres",
contact_info = "Call me!"
)
cls.shipment1 = Purchase.objects.create(
tag=9,
shipment_id=9999,
supplier_id = cls.supplier1,
purchase_date='2015-01-09',
purchase_price=9.99,
product_name=cls.variety1,
pieces=99,
kgs=999,
crackout_estimate=99.9
)
cls.shipment2 = Purchase.objects.create(
tag=8,
shipment_id=8888,
supplier_id=cls.supplier1,
purchase_date='2015-01-08',
purchase_price=8.88,
product_name=cls.variety1,
pieces=88,
kgs=888,
crackout_estimate=88.8
)
cls.shipment3 = Purchase.objects.create(
tag=7,
shipment_id=7777,
supplier_id=cls.supplier1,
purchase_date='2014-01-07',
purchase_price=7.77,
product_name=cls.variety1,
pieces=77,
kgs=777,
crackout_estimate=77.7
)
cls.sortrecord1 = SortingRecords.objects.create(
tag=cls.shipment1,
date="2015-02-05",
bags_sorted=20,
turnout=199,
)
cls.sortrecord2 = SortingRecords.objects.create(
tag=cls.shipment1,
date="2015-02-07",
bags_sorted=40,
turnout=399,
)
cls.sortrecord3 = SortingRecords.objects.create(
tag=cls.shipment1,
date='2015-02-09',
bags_sorted=30,
turnout=299,
)
感谢 @bruno desthuilliers 我发现了这个问题。
我的 setUpTestData()
方法是用字符串而不是日期时间对象填充字段。将它们转换为适当的输入后它可以正常工作:
purchase_date=datetime.date(2015,1,9)
在我的模型中,我有以下方法:
def _bags_remaining(self):
current_set = SortingRecords.objects.values().filter(~Q(id=self.id), tag=self.tag)
sorted = [SortingRecords['bags_sorted'] for SortingRecords in current_set if
SortingRecords['date'] <= self.date]
remaining = self.tag.pieces - sum(sorted) - self.bags_sorted
return remaining
bags_remaining = property(_bags_remaining)
它旨在查找到目前为止在与记录关联的标签下分类的行李数量,并从行李总数中扣除该数量(连同在该记录下分类的数量)。
效果很好!适当的金额已成功传递到模板。
然而,令我失望的是它取消了我的单元测试。
======================================================================
ERROR: test_sorting_records_bags_remaining_calculation (AlmondKing.InventoryLogs.tests.test_views.test_purchase_details.DetailsPageTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\tests\test_views\test_purchase_details.py", line 155, in test_sorting_records_bags_remaining_calculation
self.assertEqual(self.sortrecord1.bags_remaining, 79)
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\models.py", line 115, in _bags_remaining
sorted = [SortingRecords['bags_sorted'] for SortingRecords in current_set if
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\models.py", line 116, in <listcomp>
SortingRecords['date'] <= self.date]
TypeError: unorderable types: datetime.date() <= str()
----------------------------------------------------------------------
Ran 22 tests in 0.203s
FAILED (errors=2)
Destroying test database for alias 'default'...
它似乎将我的日期对象解释为字符串。它从中提取的模型是 DateField。如果我在上面调用类型,它会报告为:
这是它所在的型号:
class SortingRecords(models.Model):
tag = models.ForeignKey(Purchase, related_name='sorting_record')
date = models.DateField()
bags_sorted = models.IntegerField()
turnout = models.IntegerField()
objects = models.Manager()
def __str__(self):
return "%s [%s]" % (self.date, self.tag.tag)
这是我的考试运行。
# Sorting Records should calculate bags remaining for each entry.
def test_sorting_records_bags_remaining_calculation(self):
self.assertEqual(self.sortrecord1.bags_remaining, 79)
self.assertEqual(self.sortrecord2.bags_remaining, 39)
self.assertEqual(self.sortrecord3.bags_remaining, 9)
同样,它在现实生活中有效,但在 运行 测试中失败了。有什么想法吗?
编辑以添加详细信息:
使用的数据库是 Postgres。
这是我的测试 setUpTestData():
class DetailsPageTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.product1 = ProductGroup.objects.create(
product_name="Almonds"
)
cls.variety1 = Variety.objects.create(
product_group = cls.product1,
variety_name = "non pareil",
husked = False,
finished = False,
)
cls.supplier1 = Supplier.objects.create(
company_name = "Acme",
company_location = "Acme Acres",
contact_info = "Call me!"
)
cls.shipment1 = Purchase.objects.create(
tag=9,
shipment_id=9999,
supplier_id = cls.supplier1,
purchase_date='2015-01-09',
purchase_price=9.99,
product_name=cls.variety1,
pieces=99,
kgs=999,
crackout_estimate=99.9
)
cls.shipment2 = Purchase.objects.create(
tag=8,
shipment_id=8888,
supplier_id=cls.supplier1,
purchase_date='2015-01-08',
purchase_price=8.88,
product_name=cls.variety1,
pieces=88,
kgs=888,
crackout_estimate=88.8
)
cls.shipment3 = Purchase.objects.create(
tag=7,
shipment_id=7777,
supplier_id=cls.supplier1,
purchase_date='2014-01-07',
purchase_price=7.77,
product_name=cls.variety1,
pieces=77,
kgs=777,
crackout_estimate=77.7
)
cls.sortrecord1 = SortingRecords.objects.create(
tag=cls.shipment1,
date="2015-02-05",
bags_sorted=20,
turnout=199,
)
cls.sortrecord2 = SortingRecords.objects.create(
tag=cls.shipment1,
date="2015-02-07",
bags_sorted=40,
turnout=399,
)
cls.sortrecord3 = SortingRecords.objects.create(
tag=cls.shipment1,
date='2015-02-09',
bags_sorted=30,
turnout=299,
)
感谢 @bruno desthuilliers 我发现了这个问题。
我的 setUpTestData()
方法是用字符串而不是日期时间对象填充字段。将它们转换为适当的输入后它可以正常工作:
purchase_date=datetime.date(2015,1,9)