Please help me decipher a traceback for "ValueError: invalid literal for int()" in my models
Please help me decipher a traceback for "ValueError: invalid literal for int()" in my models
我正在尝试创建 ProductPart
个对象的列表,其中 ProductPart.part
与 Variety.variety_name
的实例值相匹配。这是我的问题的演示。
> v = Variety.objects.get(id=2)
> p = ProductPart.objects.get(id=1)
> v.variety_name
'Non Pareil (Inshell)'
> p.part
<Variety: Non Pareil (Inshell)>
> a = ProductPart.objects.values().filter(part=v.variety_name)
...
ValueError: invalid literal for int() with base 10: 'Non Pareil (Inshell)'
此模式似乎适用于我的模型的其他地方,但出于某种原因,它不适用于此处。有谁知道我做错了什么?有人能帮我理解为什么它要获取 int() 对象吗?
回溯:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\models.py", line 49, in _total_sales_of_variety
sold = ProductPart.objects.values().filter(part=self.variety_name)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\query.py", line 679, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\query.py", line 697, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\sql\query.py", line 1304, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\sql\query.py", line 1332, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\sql\query.py", line 1194, in build_filter
lookups, value)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\fields\related.py", line 1740, in get_lookup_constraint
ookup_class(target.get_col(alias, source), val), AND)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\lookups.py", line 96, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\lookups.py", line 134, in get_prep_lookup
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\fields\__init__.py", line 727, in get_prep_lookup
return self.get_prep_value(value)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\fields\__init__.py", line 985, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Non Pareil (Inshell)'
型号:
class Variety(models.Model):
product_group = models.ForeignKey(ProductGroup)
variety_name = models.CharField(max_length=140)
husked = models.BooleanField()
finished = models.BooleanField() #defunct
description = models.CharField(max_length=500, blank=True)
class ProductPart(models.Model):
product = models.ForeignKey(Product)
part = models.ForeignKey(Variety)
qty = models.DecimalField(max_digits=28, decimal_places=2)
您正在尝试将外键与字符串匹配,您需要提供模型
ProductPart.objects.values().filter(part=v)
注意:values
也不是必需的
ProductPart.objects.filter(part=v)
来自 Values
的文档
a QuerySet subclass that returns dictionaries when used as an iterable, rather than model-instance objects.
换句话说,您正在失去对当前方法的对象的一些控制
根据 Django ORM,您应该直接在过滤器中处理模型对象(它使代码易于阅读)
v = Variety.objects.get(id=2)
a = ProductPart.objects.filter(part=v)
这是当您尝试将字符串解析为 int 并且不是有效整数时遇到的错误:int('hello')
您正试图在此查询中做一些奇怪的事情:
a = ProductPart.objects.values().filter(part=v.variety_name)
基本上,part
是 Variety
的外键(在这种情况下,外键是一个整数),您正试图将它与字符串 v.variety_name
进行比较。
我认为正确的做法是这样的:
a = ProductPart.objects.filter(part=v)
你应该使用:
a = ProductPart.objects.values().filter(part__variety_name=v.variety_name)
查看此 Django 文档
Lookups that span relationships
我正在尝试创建 ProductPart
个对象的列表,其中 ProductPart.part
与 Variety.variety_name
的实例值相匹配。这是我的问题的演示。
> v = Variety.objects.get(id=2)
> p = ProductPart.objects.get(id=1)
> v.variety_name
'Non Pareil (Inshell)'
> p.part
<Variety: Non Pareil (Inshell)>
> a = ProductPart.objects.values().filter(part=v.variety_name)
...
ValueError: invalid literal for int() with base 10: 'Non Pareil (Inshell)'
此模式似乎适用于我的模型的其他地方,但出于某种原因,它不适用于此处。有谁知道我做错了什么?有人能帮我理解为什么它要获取 int() 对象吗?
回溯:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Projects\AlmondKing\AlmondKing\InventoryLogs\models.py", line 49, in _total_sales_of_variety
sold = ProductPart.objects.values().filter(part=self.variety_name)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\query.py", line 679, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\query.py", line 697, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\sql\query.py", line 1304, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\sql\query.py", line 1332, in _add_q
allow_joins=allow_joins, split_subq=split_subq,
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\sql\query.py", line 1194, in build_filter
lookups, value)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\fields\related.py", line 1740, in get_lookup_constraint
ookup_class(target.get_col(alias, source), val), AND)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\lookups.py", line 96, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\lookups.py", line 134, in get_prep_lookup
return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\fields\__init__.py", line 727, in get_prep_lookup
return self.get_prep_value(value)
File "C:\Users\Adam\Envs\AlmondKing\lib\site-packages\django\db\models\fields\__init__.py", line 985, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'Non Pareil (Inshell)'
型号:
class Variety(models.Model):
product_group = models.ForeignKey(ProductGroup)
variety_name = models.CharField(max_length=140)
husked = models.BooleanField()
finished = models.BooleanField() #defunct
description = models.CharField(max_length=500, blank=True)
class ProductPart(models.Model):
product = models.ForeignKey(Product)
part = models.ForeignKey(Variety)
qty = models.DecimalField(max_digits=28, decimal_places=2)
您正在尝试将外键与字符串匹配,您需要提供模型
ProductPart.objects.values().filter(part=v)
注意:values
也不是必需的
ProductPart.objects.filter(part=v)
来自 Values
a QuerySet subclass that returns dictionaries when used as an iterable, rather than model-instance objects.
换句话说,您正在失去对当前方法的对象的一些控制
根据 Django ORM,您应该直接在过滤器中处理模型对象(它使代码易于阅读)
v = Variety.objects.get(id=2)
a = ProductPart.objects.filter(part=v)
这是当您尝试将字符串解析为 int 并且不是有效整数时遇到的错误:int('hello')
您正试图在此查询中做一些奇怪的事情:
a = ProductPart.objects.values().filter(part=v.variety_name)
基本上,part
是 Variety
的外键(在这种情况下,外键是一个整数),您正试图将它与字符串 v.variety_name
进行比较。
我认为正确的做法是这样的:
a = ProductPart.objects.filter(part=v)
你应该使用:
a = ProductPart.objects.values().filter(part__variety_name=v.variety_name)
查看此 Django 文档 Lookups that span relationships