Django错误:/new_bid/1处的ValueError无法分配"1":"Bid.listingid"必须是“列表"实例 [英] Django Error: ValueError at /new_bid/1 Cannot assign "1": "Bid.listingid" must be a "Listings" instance

查看:48
本文介绍了Django错误:/new_bid/1处的ValueError无法分配"1":"Bid.listingid"必须是“列表"实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误:/new_bid/1处的ValueError无法分配"1":"Bid.listingid"必须是列表"实例."

I am getting the error: ValueError at /new_bid/1 Cannot assign "1": "Bid.listingid" must be a "Listings" instance."

我仍在学习外键,所以我可能用错了?我还在模型列表"和出价"中都使用了"listingid".我想知道这是否是问题的一部分,但同时兼顾我和我是有意义的.

I am still learning about Foreign Keys so I may have used it wrong? I'm also using 'listingid' in both the models 'Listings' and 'Bid.' I'm wondering if this is part of the issue, but it makes sense to have both to me.

我只是在学习自我"的工作原理,所以我认为部分问题在于最底层的列表"模型中:

I am just learning about how 'self' works also, so I think part of the problem is in the Listings model at the very bottom:

在清单模型中,如果执行以下操作:返回self.last_bid,则会收到错误''>''int'和'Bid'''实例之间不受支持;如果保持原样,则会在此Stack Overflow帖子的标题中收到错误消息.

In the Listings model if I do: return self.last_bid I get the error "'>' not supported between instances of 'int' and 'Bid'" and if I keep it as is now, I get the error in the title of this Stack Overflow post.

感谢您的帮助,我无法理解该错误.

Any help is appreciated, I'm having trouble understanding the error.

views.py

def new_bid(request, listingid):
    if request.method == "POST":
        listing = Listings.objects.get(pk=listingid)
        response = redirect("listingpage", listingid=listingid)
        try:
            bid = int(request.POST["bid"])
        except ValueError:
            response.set_cookie(
                "message", "Please input something before submitting the bid", max_age=3
            )
            return response
        if bid > listing.current_price():
            response.set_cookie("message", "Your bid was accepted", max_age=3)
            Bid.objects.create(bid=bid, listingid=listingid, user=request.user)
        else:
            response.set_cookie(
                "message", "Your bid should be higher than the current bid", max_age=3
            )
        return response
    else:
        return redirect("index")

models.py

models.py

class Bid(models.Model):
    user = models.CharField(max_length=64)
    title = models.CharField(max_length=64)
    bid = models.IntegerField(blank=True)
    listingid = models.ForeignKey('Listings', on_delete=models.CASCADE, related_name='listing_for_bid')

class Listings(models.Model):
    listingid = models.IntegerField(blank=True, null=True)
    description = models.TextField(max_length=250)
    starting_bid = models.IntegerField()
    bids = models.ManyToManyField('Bid', related_name='bids_in_the_auction', blank=True)
    last_bid = models.ForeignKey('Bid', on_delete=models.CASCADE, related_name='last_bid_for_the_auction', blank=True,
                             null=True)
  def current_price(self):
    return self.starting_bid

推荐答案

而不是使用它:

def new_bid(request, listingid):
    if request.method == "POST":
        listing = Listings.objects.get(pk=listingid)
        # your working code.....
       
        if bid > listing.current_price():
            response.set_cookie("message", "Your bid was accepted", max_age=3)
            Bid.objects.create(bid=bid, listingid=listingid, user=request.user) 
        # your remaining code.......

使用此:

def new_bid(request, listingid):
    if request.method == "POST":
        listing = Listings.objects.get(pk=listingid) # refer this object to create new bid
        # your working code here...........
        if bid > listing.current_price():
            response.set_cookie("message", "Your bid was accepted", max_age=3)
            Bid.objects.create(bid=bid, listingid=listing, user=request.user)
        # remaining code......

希望这会起作用.您可以在此处>>了解更多信息.

Hope this will work. You can read more about this here.

这篇关于Django错误:/new_bid/1处的ValueError无法分配"1":"Bid.listingid"必须是“列表"实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆