Django Error: ValueError at /new_bid/1 Cannot assign “1”: “Bid.listingid” must be a “Listings” instance

放肆的年华 提交于 2021-01-29 09:33:16

问题


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

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:

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

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

回答1:


Instead of using this:

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.......

Use this:

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.



来源:https://stackoverflow.com/questions/65839225/django-error-valueerror-at-new-bid-1-cannot-assign-1-bid-listingid-must-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!