私はデータベースの構造が枯渇しています。私はProductBidPrice
クラスを作成します。すべての列を追加しても問題はありませんが、1つの列はout_going_price
とincome_price
です。私は新しいProductBidPrice
ジャンゴを保存すると、このエラーにdjango UNIQUE制約に失敗しました。エラー
を投げる "UNIQUE制約に失敗しました:sales_productbidprice.price_income_id"
を。私は1対1のrealtionshipを使用したい。
私はDjango Web Interfaceを追加して保存できます。しかし、私はビューに追加することはできません。
この問題を解決するにはどうすればよいですか?
私の英語については申し訳ありません。私は私の問題を説明してほしい。
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同じ番号がデータベースに存在する挿入しようとする場合OneToOneFieldとして、このモデルは、エラーが発生します
@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})