django UNIQUE约束失败错误

时间:2017-12-08 08:24:23

标签: python django pycharm one-to-one

我有数据库的结构。我在视图上创建了ProductBidPrice类。添加所有列没有任何问题,只有一列out_going_priceincome_price。当我保存新的ProductBidPrice django时抛出此错误

  

“UNIQUE约束失败:sales_productbidprice.price_income_id”

。我想要使​​用多个一对一的关系。

我可以添加和保存Django Web界面。但我不能在视图中添加。

如何解决此问题?

抱歉我的英文。我希望解释一下我的问题。

models.py

    class ProductPriceHistory(BaseModel):
        currency = models.ForeignKey(PriceCurrency)
        price = models.FloatField()
        date = models.DateField()

        class Meta:
            abstract = True


    class ProductIncomePriceHistory(ProductPriceHistory):
        product = models.ForeignKey(Product, related_name="prices_income")

        def __str__(self):
            return "%s %s of %s" % (self.price, self.currency.name, self.product.name)


    class ProductOutgoingPriceHistory(ProductPriceHistory):
        product = models.ForeignKey(Product, related_name="prices_outgoing")

        def __str__(self):
            return "%s %s of %s" % (self.price, self.currency.name, self.product.name)


class AbstractBidDirectSales(BaseModel):
    name = models.CharField(max_length=45)
    sales_date = models.DateField()
    customer = models.ForeignKey(Customer)

    class Meta:
        abstract = True


    class Bid(AbstractBidDirectSales):
        products = models.ManyToManyField(Product, related_name="bids", through="ProductBidPrice")

        def __str__(self):
            return "%s of %s" % (self.name, self.customer.name)


    class DirectSale(AbstractBidDirectSales):
        product = models.ManyToManyField(Product, related_name="directSales",  through="ProductDirectSalesPrice")

        class Meta:
            verbose_name_plural = "DirectSales"

        def __str__(self):
            return "%s of %s" % (self.name, self.customer.name)


    class ProductDirectSalesPrice(BaseModel):
        product = models.ForeignKey(Product)
        directSales = models.ForeignKey(DirectSale)
        price_income = models.OneToOneField(ProductIncomePriceHistory)
        price_outgoing = models.OneToOneField(ProductOutgoingPriceHistory)
        item_number = models.IntegerField()
        piece = models.IntegerField()

        def __str__(self):
            return "%s of %s %s" % (self.product, self.bid.name, self.piece)


    class ProductBidPrice(BaseModel):
        product = models.ForeignKey(Product)
        bid = models.ForeignKey(Bid)
        price_income = models.OneToOneField(ProductIncomePriceHistory)
        price_outgoing = models.OneToOneField(ProductOutgoingPriceHistory)
        item_number = models.IntegerField()
        piece = models.IntegerField()

        def __str__(self):
            return "%s of %s %s" % (self.product, self.bid.name, self.piece)

views.py

@login_required(login_url="/login/")
def add_bid(request):
    if request.method == "POST":
        new_bid = Bid();
        new_bid.name = request.POST["name"];
        new_bid.sales_date = request.POST["date"];
        new_bid.customer_id = request.POST["customerSelection"];
        new_bid.save();
        price = request.POST;
        items = [];
        pieces = [];
        ubb_code = [];
        for q in price:
            if q.startswith("item"):
                items.append(q);
            if q.startswith("piece"):
                pieces.append(q);
            if q.startswith("productSelection"):
                ubb_code.append(q);
        items = sorted(items);
        pieces = sorted(pieces);
        ubb_code = sorted(ubb_code);

        for i in range(len(items)):
            new_bid_product = ProductBidPrice();
            new_bid_product.bid = new_bid;
            new_bid_product.product_id = request.POST[ubb_code[i]];
            new_bid_product.item_number = request.POST[items[i]];
            new_bid_product.piece = request.POST[pieces[i]];
            income_price = ProductIncomePriceHistory.objects.filter(product_id= request.POST[ubb_code[i]]);
            outgoing_price = ProductOutgoingPriceHistory.objects.filter(product_id=request.POST[ubb_code[i]]);
            new_bid_product.price_income_id = income_price[0].id;
            new_bid_product.price_outgoing_id = outgoing_price[0].id;
            new_bid_product.save();

    customers = Customer.objects.all();
    products = Product.objects.all();
    return render(request, "addBid.html", {"customers": customers, "products":products})

2 个答案:

答案 0 :(得分:2)

您已将此字段定义为一对一关系。

   class ProductBidPrice(BaseModel):
    product = models.ForeignKey(Product)
    bid = models.ForeignKey(Bid)
    price_income = models.OneToOneField(ProductIncomePriceHistory)

因此,只有一个ProductBidPrice可以有一个ProductIncomePriceHistory可能会引发错误,因为您已经有一个ProductBidPrice,而您正在尝试使用ProductIncomePriceHistory.id。

如果我正在解释你正在尝试做的事情,我相信你想要很多关系。

答案 1 :(得分:1)

class ProductDirectSalesPrice(BaseModel):
        product = models.ForeignKey(Product)
        directSales = models.ForeignKey(DirectSale)
        price_income = models.ForeignKey(ProductIncomePriceHistory)
        price_outgoing = models.ForeignKey(ProductOutgoingPriceHistory)
        item_number = models.IntegerField()
        piece = models.IntegerField()

        def __str__(self):
            return "%s of %s %s" % (self.product, self.bid.name, self.piece)


    class ProductBidPrice(BaseModel):
        product = models.ForeignKey(Product)
        bid = models.ForeignKey(Bid)
        price_income = models.ForeignKey(ProductIncomePriceHistory)
        price_outgoing = models.ForeignKey(ProductOutgoingPriceHistory)
        item_number = models.IntegerField()
        piece = models.IntegerField()

使用此模型,因为如果尝试插入数据库中存在的相同数字,OneToOneField将引发错误