I am trying to create a list of ProductPart
objects where ProductPart.part
matches the value of an instance of Variety.variety_name
. Here is a demonstration of my problem.
> 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)'
This pattern seems to work elsewhere in my models, but for some reason it isn't working here. Does anyone know what I am doing wrong? Can someone help me understand why it is reaching for an int() object?
Traceback:
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)'
Models:
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)
You are trying to match a foreign key to a string, you need to provide the model
Note: the
values
isn't necessary eitherFrom the documentation for
Values
In other words, you are losing some control over your objects with your current approach