/ api / shopping-cart /的IntegrityError(1048,“列'product_id'不能为空”)

时间:2019-03-15 17:29:17

标签: django-rest-framework angular6

我有三种模式。产品,购物车和购物车详细信息。当我将产品添加到购物车时,我想在ShoppingCart和CartDetails中保存数据.ShoppingCart(id)与CartDetails(cart_id)相关。而CartDetails(product_id)与Product(id)相关。现在,当我发布数据时,它显示错误“列'product_id'不能为空”。我想我错过了一件愚蠢的事情。但是却找不到傻事。我是django rest框架的新手,我在前端使用了angular。我不明白自己在做什么错。

models.py

class Product(models.Model):
    product_name = models.CharField(max_length=200)
    cat_id = models.ForeignKey('Category', on_delete=models.CASCADE, db_column='cat_id')
    description = models.TextField()
    image_url = models.ImageField(upload_to='images/product_name/%Y/%m/%d/')
    sell_price = models.IntegerField()
    cost_price = models.IntegerField()
    discount_price = models.IntegerField()
    active = models.BooleanField()
    create_date = models.DateTimeField(default=datetime.now)

    def __str__(self):
        return self.product_name

    class Meta:
        db_table = "products"


    class ShoppingCart(models.Model):
        create_date = models.DateTimeField(default=datetime.now)
        amount = models.IntegerField(default=0)

    def __int__(self):
        return self.id

    class Meta:
        db_table = "shopping-cart"


class CartDetails(models.Model):
    cart = models.ForeignKey(ShoppingCart, related_name='cart_details', on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    qty = models.IntegerField(default=0)
    amount = models.IntegerField(default=0)
    create_date = models.DateTimeField(default=datetime.now)

    def __int__(self):
        return self.id

    class Meta:
        db_table = "cart-details"

serializers.py

class ProductSerializer(serializers.ModelSerializer):
    image_url = Base64ImageField(max_length=None, use_url=True)

    class Meta:
        model = Product
        fields = ('id', 'product_name', 'description', 'image_url', 'sell_price', 'cost_price',
                  'discount_price', 'active', 'cat_id')


class CartDetailSerializer(serializers.ModelSerializer):

class Meta:
    model = CartDetails
    fields = ('product', 'qty', 'amount')


class ShoppingCartSerializer(serializers.ModelSerializer):
    cart_details = CartDetailSerializer()

    class Meta:
        model = ShoppingCart
        fields = ('id', 'amount', 'cart_details')

    def create(self, validated_data):
        carts_data = validated_data.pop('cart_details')
        # cart = ShoppingCart.objects.create(**validated_data)
        #  for data in carts_data:
        # CartDetails.objects.create(cart=cart, **carts_data)
        return validated_data

views.py

class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer


class ShoppingCartViewSet(viewsets.ModelViewSet):
    queryset = ShoppingCart.objects.all()
    serializer_class = ShoppingCartSerializer


class CartDetailsViewSet(viewsets.ModelViewSet):
    queryset = CartDetails.objects.all()
    serializer_class = CartDetailSerializer

这是发布数据的有角服务

private create(product) {
    const cart = {
      amount: product.sell_price,
      cart_details: {
        qty: 1,
        product: product.id,
        amount: product.sell_price
      }
    };
    return this.http.post(this.globalService.baseUrl + 'shopping-cart/', cart);
  }

1 个答案:

答案 0 :(得分:0)

如果我没记错,问题就在这里

product_id = ProductSerializer(read_only=True, source='product.id')

,因为您将其设置为read_only字段,所以product_id不会在实例创建时写入。 read_only应该被删除。

product_id = ProductSerializer(source='product.id')