异常 int() argument must be a string or a number, not 'ShopCar'

出错信息

Traceback:
File "E:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\learngit\store\auto_sale\views.py" in add_to_shop_car
  82.                                                       size_name=size.name, color_name=size.name)
File "E:\Python27\lib\site-packages\django\db\models\manager.py" in manager_method
  127.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "E:\Python27\lib\site-packages\django\db\models\query.py" in get_or_create
  405.             return self.get(**lookup), False
File "E:\Python27\lib\site-packages\django\db\models\query.py" in get
  325.         clone = self.filter(*args, **kwargs)
File "E:\Python27\lib\site-packages\django\db\models\query.py" in filter
  679.         return self._filter_or_exclude(False, *args, **kwargs)
File "E:\Python27\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  697.             clone.query.add_q(Q(*args, **kwargs))
File "E:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_q
  1309.         clause, require_inner = self._add_q(where_part, self.used_aliases)
File "E:\Python27\lib\site-packages\django\db\models\sql\query.py" in _add_q
  1337.                     allow_joins=allow_joins, split_subq=split_subq,
File "E:\Python27\lib\site-packages\django\db\models\sql\query.py" in build_filter
  1199.                                                     lookups, value)
File "E:\Python27\lib\site-packages\django\db\models\fields\related.py" in get_lookup_constraint
  1756.                     lookup_class(target.get_col(alias, source), val), AND)
File "E:\Python27\lib\site-packages\django\db\models\lookups.py" in __init__
  101.         self.rhs = self.get_prep_lookup()
File "E:\Python27\lib\site-packages\django\db\models\lookups.py" in get_prep_lookup
  139.         return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
File "E:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_lookup
  727.             return self.get_prep_value(value)
File "E:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
  985.         return int(value)

Exception Type: TypeError at /add_to_shop_car/
Exception Value: int() argument must be a string or a number, not 'ShopCar'
Request information:
GET: No GET data

代码:

# 加入购物车
    shop_car = ShopCar.objects.get_or_create(user=user)
    shop_car_item = ShopCarItem.objects.get_or_create(shop_car=shop_car, product=product, size=size, color=color,
                                                      size_name=size.name, color_name=color.name)
    shop_car_item.count = shop_car_item.count + count
    shop_car_item.save()

解决:

# 加入购物车
    shop_car = ShopCar.objects.get_or_create(user=user)[0]
    shop_car_item = ShopCarItem.objects.get_or_create(shop_car=shop_car, product=product, size=size, color=color,
                                                      size_name=size.name, color_name=color.name)[0]
    shop_car_item.count = shop_car_item.count + count
    shop_car_item.save()

get_or_create得到的tuple类型
ShopCarItem.objects.get_or_create中shop_car传入tuple类型,导致在数据库底层中需要得到外键id时,得到ShopCar类型,而不是int类型
 
  

你可能感兴趣的:(django)