我创建了两个模型“ Restaurantmenu ”和“购物车”,其中Restaurantmenu.Product_id是主键,Cart.prdct_id是外键。 迁移完成没有任何错误。但是当我在购物车中插入数据时,我收到了一个错误:
ValueError:无法指定“'4359197'”:“Cart.prdct_id”必须是“Restaurantmenu”实例。
找到模型文件:
class Restaurantmenu(models.Model):
product_id = models.BigIntegerField(primary_key=True)
shopid = models.CharField(max_length=50)
prod_type = models.CharField(max_length=50,null=True)
prd_category = models.CharField(max_length=50,null=True)
prd_name = models.CharField(max_length=100,null=True)
prd_amt = models.FloatField(null=True)
prod_desc = models.CharField(max_length=200,null=True)
offer = models.FloatField(null=True,default=0)
offer_type= models.CharField(max_length=50,null=True,default='None')
status = models.BooleanField(default=1)
image = models.CharField(max_length=200)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now = True)
def __unicode__(self):
return self.product_id
class Cart(models.Model):
cart_id = models.BigIntegerField(null=True)
cust_id = models.IntegerField(null=True)
prdct_id = models.ForeignKey(Restaurantmenu,on_delete=models.DO_NOTHING)
no_of_quantity = models.IntegerField(null=True)
total_amt = models.FloatField(null=True)
status = models.CharField(max_length=50,null=True)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.cart_id
其REST API视图:
def addcart(self,requestobj,cart_amt):
cartid = self.generate_cart_id(7)
print(type(int(requestobj['pid'])))
try:
cartobj,cartcreate = Cart.objects.get_or_create(
cart_id = cartid,
cust_id = requestobj['Userid'],
prdct_id= int(requestobj['pid']),
defaults={
'no_of_quantity':requestobj['quantity'],
'total_amt': cart_amt,
'status':'Pending'
}
)
if cartcreate:
response = {"Status":"Success","Message":"Added to the cart","code":"200"}
else:
response = {"Status":"Error","Message":"Not Added, Try Again","code":"100"}
except MultipleObjectsReturned:
response = {"Status":"Error","Message":"Cart Already Exist","code":"100"}
return response
def generate_cart_id(self,len):
cartid = ""
for i in range(len):
rnd_nbr = str(random.randint(1,9))
cartid+=str(rnd_nbr)
return cartid
答案 0 :(得分:0)
您将prdct_id设置为外键,该外键使用数据库中的ID,但Django ORM需要Restaurantmenu
的实例。
因此,您可能需要添加以下内容:
menu = Restaurantmenu.objects.get(product_id = int(requestobj['pid']))
获取对该菜单对象的引用,
然后在你的get_or_create中,像这样使用它:
cartobj,cartcreate = Cart.objects.get_or_create(
cart_id = cartid,
cust_id = requestobj['Userid'],
prdct_id= menu,
defaults={
'no_of_quantity':requestobj['quantity'],
'total_amt': cart_amt,
'status':'Pending'
}
)
答案 1 :(得分:0)
当您向模型中添加外键时,django会在数据库表中包含'字段' _id。例如在你的模特中
class Menu(Model):
pass
class Cart(Model):
menu = ForeignKey(Menu, CASCADE)
django在购物车模型实例中添加两个属性,菜单是一个菜单实例, menu_id 是一个整数,其中包含菜单实例ID。 django还在Menu实例中包含 cart_set 属性。
为了将一个购物车添加到菜单中,我需要添加一个新购物车并设置菜单,但是使用菜单属性django期望一个Menu实例不是id或pk
mi_menu = Menu.objects.create()
cart = Cart.objects.create(menu=mi_menu)
如果你想使用主键你需要使用为django添加的menu_id,例如
mi_menu = Menu.objects.create()
cart = Cart.objects.create(menu_id=mi_menu.pk)
您必须确保db。中存在菜单ID。
在您的代码中托盘以创建具有id的相关对象,但django期望模型实例。
答案 2 :(得分:0)
在您的代码中,prdct_id
引用FK
对象并且它期望Product
实例。因此,您必须提供 prdct_id=Restaurantmenu.object.get(id=int(requestobj['pid'])
或 prodct_id_id=int(requestobj['pid']
因此,请尝试以下代码,
def addcart(self, requestobj, cart_amt):
# your code
try:
cartobj, cartcreate = Cart.objects.get_or_create(
cart_id=cartid,
cust_id=requestobj['Userid'],
prdct_id_id= int(requestobj['pid']),
defaults = {
'no_of_quantity': requestobj['quantity'],
'total_amt': cart_amt,
'status': 'Pending'
}
)
# your code
return response