社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

django:当我从一个用户的购物车中删除项目时,相同的项目将从每个用户的购物车中删除

gigamarr • 6 年前 • 1807 次点击  

使用Python3.7

当我将项目添加到购物车时,项目将单独添加到每个用户,但我在购物车详细信息页面上有delete按钮,并且 VIEW 看起来像这样:

def delete_cart_item(request, item_id):
    user_profile = get_object_or_404(User_Profile, user=request.user)
    shopping_cart = user_profile.shopping_cart_set.first()
    item = shopping_cart.items.get(pk=item_id)
    item.delete()
    return redirect('Sales:cart_details')

这些是我的模特:

class User_Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

class Item(models.Model):
    item_name = models.CharField(max_length=200)
    item_price = models.IntegerField()
    item_description = models.CharField(max_length=300)
    item_bought_price = models.IntegerField()
    stock_level = models.IntegerField()
    restock_level = models.IntegerField()

class Cart_Item(models.Model):
    item = models.OneToOneField(Item, on_delete=models.SET_NULL, null=True)


class Shopping_Cart(models.Model):
    ref_code = models.CharField(max_length=15)
    owner = models.ForeignKey(User_Profile, on_delete=models.SET_NULL, null=True)
    items = models.ManyToManyField(Cart_Item)

这里有什么问题?我试着改变 on_delete=models.SET_NULL 属于 Cart_Item on_delete=models.CASCADE 但这似乎没有改变什么。

还有一个奇怪的问题,当购物车为空并且我添加了第一个项目时,该项目不能从页面中删除,但我可以从shell中删除它。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43227
 
1807 次点击  
文章 [ 1 ]  |  最新文章 6 年前