gpt4 book ai didi

python - Django ValueError : Field 'id' expected a number but got 'S'

转载 作者:行者123 更新时间:2023-12-05 00:55:12 28 4
gpt4 key购买 nike

下面是我的模型,用于查看错误发生的地方,尤其是 StockQuantity 模型中发生错误的地方,当我尝试过滤或使用 get 检索查询时,它显示的是预期数字但得到了“字符串值”

models.py

# Item
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
discount_price = models.FloatField(blank=True, null=True)
category = models.ForeignKey(
'Category', on_delete=models.CASCADE, null=True)
label = models.CharField(choices=LABEL_CHOICES, max_length=1)
slug = models.SlugField()
description = models.TextField()
# stock_quantity = models.IntegerField(blank=True, null=True)
cover_image = models.ImageField(blank=True, null=True,
upload_to=item_cover_upload_location, default='no-product-image.jpg')

is_footwear = models.BooleanField(default=False)
upload_date = models.DateTimeField(default=timezone.now)

# StockQuantity
class StockQuantity(models.Model):
item = models.ForeignKey('Item', on_delete=models.CASCADE, null=True)
color = models.ForeignKey(
'ItemColor', on_delete=models.CASCADE)
cloth_size = models.ForeignKey(
'ClothSize', on_delete=models.CASCADE, blank=True, null=True)
footwear_size = models.ForeignKey(
'FootwearSize', on_delete=models.CASCADE, blank=True, null=True)
stock_quantity = models.IntegerField(null=True, default=10)

#OrderItem
class OrderItem(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
item = models.ForeignKey(Item, on_delete=models.CASCADE)
quantity = models.IntegerField(default=1)
footwear_size = models.CharField(max_length=50, null=True)
cloth_size = models.CharField(max_length=50, null=True)
color = models.CharField(max_length=50, null=True)
ordered = models.BooleanField(default=False)

#Order
class Order(models.Model):
user = models.ForeignKey(
settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
ref_code = models.CharField(max_length=20, blank=True, null=True)
items = models.ManyToManyField(OrderItem)
start_date = models.DateTimeField(auto_now_add=True)
ordered_date = models.DateTimeField()
ordered = models.BooleanField(default=False)
shipping_address = models.ForeignKey(
'Address', related_name='shipping_address', on_delete=models.SET_NULL, blank=True, null=True)
billing_address = models.ForeignKey(
'Address', related_name='billing_address', on_delete=models.SET_NULL, blank=True, null=True)
payment = models.ForeignKey(
'Payment', on_delete=models.SET_NULL, blank=True, null=True)
coupon = models.ForeignKey(
'Coupon', on_delete=models.SET_NULL, blank=True, null=True)
being_delivered = models.BooleanField(default=False)
received = models.BooleanField(default=False)
requested_refund = models.BooleanField(default=False)
refund_status = models.BooleanField(default=False)

#Payment
class Payment(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
on_delete=models.SET_NULL, blank=True, null=True)
paypal_transaction_id = models.CharField(max_length=50)
amount = models.FloatField()
timestamp = models.DateTimeField(default=timezone.now)

# ItemColor
class ItemColor(models.Model):
item_color = models.CharField(
max_length=25, null=True, blank=True)
item = models.ForeignKey('Item', on_delete=models.CASCADE, null=True)

def __str__(self):
return self.item_color

# ClothSize
class ClothSize(models.Model):
cloth_size = models.CharField(
max_length=25, null=True, blank=True)
item = models.ForeignKey('Item', on_delete=models.CASCADE, null=True)

def __str__(self):
return self.cloth_size

# FootwearSize
class FootwearSize(models.Model):
footwear_size = models.CharField(
max_length=25, null=True, blank=True)
item = models.ForeignKey('Item', on_delete=models.CASCADE, null=True)

以下是我的views.py,特别是引发错误的 View ,我已发表评论以进一步帮助确认错误发生的位置

Views.py

# View where the error occurs
def payment_complete(request):
body = json.loads(request.body)

order = Order.objects.get(
user=request.user, ordered=False, ref_code=body['orderid'])
payment = Payment(
user=request.user,
paypal_transaction_id=body['payid'],
amount=order.get_total()
)
payment.save()
order.ordered = True
order.payment = payment
order.received = True
order.save()
order_items = order.items.all()
order_items.update(ordered=True)

for item in order_items:
if item.item.is_footwear:\
# ERROR OCCURS BELOW ---------
st = StockQuantity.objects.get(
item=item.item, color=item.color, footwear_size=str(item.footwear_size))
else:
# ERROR OCCURS BELOW ---------
st = StockQuantity.objects.get(
item=item.item, color=item.color, cloth_size=str(item.cloth_size))
if st.stock_quantity > 0:
st.stock_quantity -= item.quantity
st.save()
item.save()
item.item.save()
else:
messages.warning("Item out of stock!!")
print("Quantity " + str(st.stock_quantity))
# order_item = OrderItem.objects.get(user=request.user, ordered=True)

messages.success(request, "Order placed successfully!!")
return redirect("/")

追溯和错误

Django version 3.1.1, using settings 'djecommerce.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[25/Nov/2020 11:23:42] "GET /product/dummy-item-1/ HTTP/1.1" 200 15564
[25/Nov/2020 11:23:45] "POST /add-to-cart/dummy-item-1 HTTP/1.1" 302 0
[25/Nov/2020 11:23:45] "GET /order-summary/ HTTP/1.1" 200 9206
[25/Nov/2020 11:23:46] "GET /checkout/ HTTP/1.1" 200 37908
Using the defualt shipping address
Using the defualt billing address
[25/Nov/2020 11:23:51] "POST /checkout/ HTTP/1.1" 302 0
[25/Nov/2020 11:23:51] "GET /payment/paypal/ HTTP/1.1" 200 11866
Internal Server Error: /payment-complete/
Traceback (most recent call last):
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'S'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\core\views.py", line 446, in payment_complete
st = StockQuantity.objects.get(
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\query.py", line 418, in get
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\query.py", line 942, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\query.py", line 962, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, *args, **kwargs)
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\query.py", line 969, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\sql\query.py", line 1358, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\sql\query.py", line 1319, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\sql\query.py", line 1165, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\lookups.py", line 24, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\fields\related_lookups.py", line 115, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "C:\Users\halfs\Desktop\DJango\Django-ecommerce\env\lib\site-packages\django\db\models\fields\__init__.py", line 1776, in get_prep_value
raise e.__class__(
ValueError: Field 'id' expected a number but got 'S'.
[25/Nov/2020 11:24:18] "POST /payment-complete/ HTTP/1.1" 500 129792
[25/Nov/2020 11:24:19] "GET / HTTP/1.1" 200 15371

我了解问题是 Django 通过分配 ID 而不是实际值来保存外键,但如果我不知道对象的 ID,我不知道如何过滤对象。谢谢,任何帮助表示赞赏!

最佳答案

查询 ForeignKey 字段时,您“通常”传递模型的一个实例。例如:

# Example models
class AnotherModel(models.Model):
name = models.CharField(...)


class MyModel(models.Model):
another_model = models.ForeignKey(AnotherModel,...)


# Get the instance
another_model_instance = AnotherModel.objects.get(id=1)
# Use the instance in the query
my_model = MyModel.objects.get(another_model=another_model_instance)

但是,您可以使用 __(双下划线)“跳跃”并查询特定字段。例如:

my_model = MyModel.objects.get(another_model__name='Some name')

在上面的示例中,我们使用 AnotherModel 模型上的 name 字段进行查询。我们可以使用它来修复您 View 中的查询。

# Taken from your payment_complete view
for item in order_items:
if item.item.is_footwear:
st = StockQuantity.objects.get(
item=item.item,
color__item_color=item.color,
footwear_size__footwear_size=str(item.footwear_size)
)
else:
st = StockQuantity.objects.get(
item=item.item,
color__item_color=item.color,
cloth_size__cloth_size=str(item.cloth_size)
)

延伸阅读:https://docs.djangoproject.com/en/3.1/topics/db/queries/ .

关于python - Django ValueError : Field 'id' expected a number but got 'S' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64999216/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com