2017-12-08 16 views
0

데이터베이스 구조가 오래되었습니다. 보기에 ProductBidPrice 클래스를 만듭니다. 모든 열을 추가해도 문제가 없지만 하나의 열은 out_going_priceincome_price입니다. 나는 새로운 ProductBidPrice 장고가django UNIQUE 제약 조건 오류가 발생했습니다.

이 오류 던져 저장하면 :

를 "UNIQUE 제약 조건이 실패를 sales_productbidprice.price_income_id". 나는 1 : 1 이상의 realtionship을 원합니다.

장고 웹 인터페이스를 추가하고 저장할 수 있습니다. 그러나 나는보기에 추가 할 수 없다.

이 문제를 어떻게 해결할 수 있습니까?

죄송합니다. 나는 내 문제를 설명하기를 희망한다.

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}) 

답변

1

이 필드를 일대일 관계로 정의했습니다.

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

그래서 하나의 ProductBidPrice 이미 사용하려고하는 ProductIncomePriceHistory.id와 ProductBidPrice을 가지고 있기 때문에 하나의 ProductIncomePriceHistory 오류가 아마 발생 할 수 있습니다.

나는 당신이 올바르게하려는 것을 해석한다면, 나는 많은 관계를 원한다고 생각합니다.

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() 

사용