我正在开发一个 Django 项目,用户可以在其中下订单,但我遇到了与外键约束相关的
IntegrityError
。
错误:
IntegrityError at /payment_getway/
FOREIGN KEY constraint failed
查看代码:
def payment_getway(request):
if request.method == "POST":
user = request.user
# Retrieve address data from the form
# address_id = request.POST.get('address_id') # Make sure you pass this ID in your form
# Retrieve the address instance
address_instance = get_object_or_404(Address, user=user)
# Create a new CartOrder instance
order = CartOrder(
user=user,
full_name=address_instance.name,
email=address_instance.email,
mobile=address_instance.mobile,
address=address_instance, # Assign the Address instance here
landmark=address_instance.landmark,
city=address_instance.region,
state=address_instance.region, # Update as per your address model
postalCode=address_instance.pin,
)
order.save()
cart_total_ammount=0
if 'cart_data_obj' in request.session:
for p_id, item in request.session['cart_data_obj'].items():
cart_total_ammount += int(item['qty']) * float(item['price'])
#getting total ammount for the cart
# for p_id, item in request.session['cart_data_obj'].items():
# cart_total_ammount += int(item['qty']) * float(item['price'])
cart_order_products = CartOrderItems.objects.create(
user=request.user,
order=order,
invoice_num= str(order.id), # INVOICE_NO-3
item=item['title'],
image=item['image'],
qty=item['qty'],
price=item['price'],
total=float(item['qty']) * float(item['price']),
# product = Product.objects.get(pid=item['pid'])
)
# Redirect or render a success page
return redirect('core:order_confirmation') # Update with your success URL
# Render payment page with context
user = request.user
addresses = Address.objects.filter(user=user)
return render(request, "core/payment-getway.html", {"addresses": addresses})
型号:
class CartOrder(models.Model):
user=models.ForeignKey(CustomUser,on_delete=models.CASCADE)
sku=ShortUUIDField(length=10,max_length=100,prefix="sku",alphabet="1234567890")
oid=ShortUUIDField(length=10,max_length=100,prefix="oid",alphabet="1234567890")
full_name=models.CharField(max_length=100,null=True,blank=True)
email=models.CharField(max_length=100,null=True,blank=True)
mobile=models.CharField(max_length=100,null=True,blank=True)
address=models.CharField(max_length=100,null=True,blank=True)
landmark=models.CharField(max_length=100,null=True,blank=True)
city=models.CharField(max_length=100,null=True,blank=True)
state=models.CharField(max_length=100,null=True,blank=True)
postalCode=models.CharField(max_length=100,null=True,blank=True)
item=models.CharField(max_length=100)
price= models.DecimalField(max_digits=10, decimal_places=2,default="0.00")
saved= models.DecimalField(max_digits=10, decimal_places=2,default="0.00")
shipping_method=models.CharField(max_length=100,null=True,blank=True)
tracking_id=models.CharField(max_length=100,null=True,blank=True)
tracking_website=models.CharField(max_length=100,null=True,blank=True)
paid_status=models.BooleanField(default=False)
stripe_payment_intent=models.CharField(max_length=1000,null=True,blank=True)
address=models.ForeignKey(Address, on_delete=models.SET_NULL, null=True, blank=True)
order_date=models.DateTimeField(auto_now_add=True)
product_status=models.CharField(choices=STATUS_CHOICE, max_length=30,default="processing")
class Meta:
verbose_name_plural="Cart Order"
class CartOrderItems(models.Model):
oid=ShortUUIDField(length=10,max_length=100,prefix="ord",alphabet="abcdef")
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
vendor = models.ForeignKey(Vendor,on_delete=models.CASCADE,default=False) #I have to fix the default
order=models.ForeignKey(CartOrder,on_delete=models.CASCADE,related_name="cartorderitems")
product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True) #added by chatgpt
invoice_num = models.BigIntegerField(blank=True,null=True)
product_status=models.CharField(max_length=200)
item=models.CharField(max_length=100)
image=models.CharField(max_length=100)
qty=models.BigIntegerField(default=0)
price= models.DecimalField(max_digits=12, decimal_places=2,default="15")
total= models.DecimalField(max_digits=12, decimal_places=2,default="20")
color=models.ForeignKey(Color,on_delete=models.SET_NULL,null=True,blank=True)
size=models.ForeignKey(Size,on_delete=models.SET_NULL,null=True,blank=True)
class Meta:
verbose_name_plural="Cart Order Items"
def catagory_image(self):
return mark_safe('<img src="%s" width="50" height="50"/>'%(self.image.url))
def oder_img(self):
return mark_safe('<img src="/media/%s" width="50" height="50"/>'%(self.image))
形式:
<div class="address-box">
<h2>Delivery Address</h2>
<p><strong>{{ address.name }}</strong></p>
<p>{{ address.address }}</p>
<p>{{ address.region }} - {{ address.pin }}</p>
<p>{{ address.mobile }}</p>
</div>
我尝试过的:
address_id
。尽管进行了这些检查,我仍然遇到
IntegrityError
。
问题:
CartOrder
实例正确关联?
任何见解或建议将不胜感激。谢谢!你没有提供错误信息,所以我提供一个简单的方法。
为所有fk声明
"db_constraint=False"
。