在Windows 10中,我正在使用react-router-dom 5.2.0和react-redux 7.2.5和react 17.0.2和axios 0.21.4和WebStorm 2023.1.3 IDE和PyCharm Community Edition 2023.2和djangorestframework==3.14.0和Django==4.2.4和djangorestframework-simplejwt==5.3.0
1.前端
考虑- manager_room.js:
{loadingGetProductCategories ? <Loader /> : errorGetProductCategories ? <div className="alert alert-danger" >{errorGetProductCategories}</div>:productCategoriesRedux ? <div className="mb-3">
<div className="form-label" htmlFor="categories">select category</div>
<select className="form-select" id="categories" value={category} options={productCategoriesRedux} onChange={(e) => handleChangeNormalSelect(e)} multiple>
{productCategoriesRedux && productCategoriesRedux.map && productCategoriesRedux.map(category => (
<option value={category.title}>{category.title}</option>
))}
</select>
</div>:""}
字符串
2.后台
考虑- product_views.py:
@api_view(http_method_names=['POST'])
@csrf_exempt
@permission_classes([IsAdminUser])
def upload_new_product(request):
try:
data = request.data
file = request.FILES
id = data.get('id')
CustomUser = get_user_model()
user = CustomUser.objects.filter(id=id).first()
print("do")
reqUser = request.user
if user == reqUser:
product = Product.objects.create(user=user, name=data.get('productName'), image=file.get('productImage'), brand=data.get('brand'), description=data.get('description'), rating=data.get('rating'), price=data.get('price'), countInStock=data.get('countInStock'))
product.categories.set(data.getlist('category'))
product.save()
srz_data = ProductSerializer(instance=product, many=False)
return Response(srz_data.data, status=status.HTTP_201_CREATED)
else:
return Response({'detail': 'you are not this account\'s user'}, status=status.HTTP_401_UNAUTHORIZED)
except APIException as err:
return Response(str(err), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
else:
print("Nothing went wrong")
finally:
print("The 'try except' is finished")
型
错误是在product.categories.set(data.getlist('category'))
行通过raise:
raise e.__class__(
ValueError: Field 'id' expected a number but got 'face picture from actor'.
[03/Jan/2024 13:39:44] "POST /api/v1/products/upload_new_product/ HTTP/1.1" 500 130849
型
实际上,保存类别没有保存在manyTmanyField
中,它显示了上述错误
考虑-models.py:
def get_product_image_path(instance, filename):
base_name = os.path.basename(filename)
name, ext = os.path.splitext(base_name)
rand_value = random.randint(1, 9999999999999999999999999999999999999)
final_name = f"{instance.name}-{instance.brand}-{instance.category}-{rand_value}-imageProduct{ext}"
return f"products_images/{instance.category}/{instance.brand}/{instance.name}/{final_name}"
class Product(models.Model):
_id = models.AutoField(primary_key=True, editable=False)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True)
name = models.CharField(max_length=200, null=True)
image = models.ImageField(upload_to=get_product_image_path, null=True, blank=True)
brand = models.CharField(max_length=200, null=True, blank=True)
category = models.CharField(max_length=200, null=True, blank=True)
description = models.TextField(null=True, blank=True)
rating = models.IntegerField(null=True, blank=True)
numReviews = models.IntegerField(null=True, blank=True, default=0)
price = models.IntegerField(null=True, blank=True)
countInStock = models.IntegerField(null=True, blank=True, default=0)
createdAt = models.DateTimeField(auto_now_add=True)
likes = models.IntegerField(null=True, blank=True, default=0)
categories = models.ManyToManyField(ProductCategory, blank=True)
def __str__(self):
return f"{str(self.name)} - {self.createdAt}"
class Meta:
verbose_name = "product"
verbose_name_plural = "products"
型
注意:* 我猜这个字段 * categories = models.ManyToManyField(ProductCategory, blank=True)
* 是**错误的中心 *。
我在这个论坛和谷歌上搜索了大部分问题,但没有找到答案。请回答我。我希望我不会遇到错字错误。
1条答案
按热度按时间nqwrtyyt1#
问题在于你如何发送类别数据,django没有expect id,但你发送的是类别名称,把它改为id。如果仍然想发送名称,试试这个:
字符串