繁体   English   中英

如何从 Django model 中排除一个值?

[英]How I can exclude a value from a Django model?

我正在尝试从监视列表中排除一个项目,但它不起作用。

我的代码:

def watchlist(request, page_id):
    items = Watchlist.objects.all()
    item = Auction_listings.objects.get(pk=page_id)

    if request.method == "POST":
        if item_id not in items:
            add = Watchlist(auction=item)
            add.save()
        else:
            remove = Watchlist.objects.get(auction=item)
            remove.delete()
            
    return render(request, "auctions/watchlist.html", {
        "items": Watchlist.objects.all()
    })

您可以检查该项目是否存在于查询集中,然后从那里检查 go。

def watchlist(request, page_id):
    items = Watchlist.objects.all()
    item = Auction_listings.objects.get(pk=page_id)

    if request.method == "POST":
        if not items.filter(auction=item).exists():
            add = Watchlist(auction=item)
            add.save()
        else:
            remove = Watchlist.objects.get(auction=item)
            remove.delete()
            
    return render(request, "auctions/watchlist.html", {
        "items": Watchlist.objects.all()
    })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM