raise e.__class__( ValueError: 字段“id”需要一个数字,但得到“演员的脸部图片”

问题描述 投票:0回答:1

windows 10 中,我正在使用 react-router-dom 5.2.0react-redux 7.2.5react 17.0.2axios 0.21.4WebStorm 2023.1.3 IDEPyCharm 社区版 2023.2djangorestframework==3.14.0Django==4.2.4djangorestframework-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)
错误的中心

我在这个论坛和Google搜索了大部分问题,但没有找到答案。请回答我。我希望我不会遇到拼写错误。

reactjs django forms django-models django-views
1个回答
0
投票

问题在于你如何发送类别数据,django orm期望id,但你发送类别名称将其更改为id。如果仍然想发送姓名,请尝试以下操作:

       category_names = data.getlist('category')
        categories = [ProductCategory.objects.get_or_create(title=category)[0] for category in category_names]

        product.categories.set(categories)
        product.save()
© www.soinside.com 2019 - 2024. All rights reserved.