unsupported operand type(s) for +: 'int' and 'str'
Internal Server Error: /product/t-shirts/
Traceback (most recent call last):
File "C:\Python311\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
self.check_response(response, callback)
File "C:\Python311\Lib\site-packages\django\core\handlers\base.py", line 332, in check_response
raise ValueError(
ValueError: The view products.views.get_product didn't return an HttpResponse object. It returned None instead.
[20/Jul/2024 12:48:11] "GET /product/t-shirts/?size=XL HTTP/1.1" 500 68697
这是我的
products.models
页面
from django.db import models
from base.models import BaseModel
from django.utils.text import slugify
class Category(BaseModel):
category_name = models.CharField(max_length=100)
slug = models.SlugField(unique=True, null=True, blank=True)
category_image = models.ImageField(upload_to="categories")
def save(self, *args, **kwargs):
self.slug = slugify(self.category_name)
super(Category, self).save(*args, **kwargs)
def __str__(self) -> str:
return self.category_name
class ColorVariant(BaseModel):
color_name = models.CharField(max_length=100)
price = models.CharField(max_length=100)
def __str__(self) -> str:
return self.color_name
class SizeVariant(BaseModel):
size_name = models.CharField(max_length=100)
price = models.CharField(max_length=100)
def __str__(self) -> str:
return self.size_name
class Product(BaseModel):
product_name = models.CharField(max_length=100)
slug = models.SlugField(unique=True, null=True, blank=True)
categorys = models.ForeignKey(
Category,
on_delete=models.CASCADE,
related_name="products",
blank=False,
null=True,
)
price = models.IntegerField()
products_description = models.TextField()
color_variant = models.ManyToManyField(ColorVariant, blank=True)
size_variant = models.ManyToManyField(SizeVariant, blank=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.product_name)
super(Product, self).save(*args, **kwargs)
def __str__(self) -> str:
return self.product_nam
def get_product_price_by_size(self, size):
return self.price + str(SizeVariant.objects.get(size_name=size).price)
class ProductImage(BaseModel):
products = models.ForeignKey(
Product, on_delete=models.CASCADE, related_name="product_images"
)
image = models.ImageField(upload_to="product")
The error message "unsupported operand type(s) for +: 'int' and 'str'" means that you are trying to add an integer and a string together, which is not allowed in Python.
Looking at your code, the problem lies in the
get_product_price_by_size
method of your
Product
model:
def get_product_price_by_size(self, size):
return self.price + str(SizeVariant.objects.get(size_name=size).price)
Here's what's happening:
-
self.price
is an integer. You defined it asprice = models.IntegerField()
in theProduct
model. -
SizeVariant.objects.get(size_name=size).price
retrieves a string. You definedprice
as aCharField
in theSizeVariant
model. -
You're trying to add the integer (
self.price
) and the string (SizeVariant.objects.get(size_name=size).price
) together. This results in the "unsupported operand type(s)" error because Python doesn't know if you want to concatenate the values as strings or add them as numbers.
To fix this, you should ensure both values are treated as numbers:
-
Change
price
inSizeVariant
to anIntegerField
: ```python class SizeVariant(BaseModel): size_name = models.CharField(max_length=100) price = models.IntegerField() # Change to IntegerFielddef str (self) -> str: return self.size_name ```
-
Modify the
get_product_price_by_size
method:python def get_product_price_by_size(self, size): size_variant = SizeVariant.objects.get(size_name=size) return self.price + size_variant.price
This modification ensures that both
self.price
and
size_variant.price
are integers before they are added together.
After making these changes, remember to run migrations:
python manage.py makemigrations
python manage.py migrate
This will update your database schema to reflect the changes you made to your model.
标签:python,django From: 78772157