我正在使用 Django createview 在拍卖网站中创建投标项目。createview 将创建新对象,但它会创建一个没有相应外键的额外对象实例。我正在使用 @staticmethod 来确定提交的出价是否确实是最高出价,然后在相关列表中创建。如果您能指出我做错了什么,请提前致谢。models.pyclass Bid(TimeStampMixin): """model representing bid obj""" auction = models.ForeignKey( Listing, on_delete=models.SET_NULL, related_name='offer', null=True) bidder = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, related_name='bid_user') amount = models.PositiveIntegerField() objects = BidQuerySet.as_manager() def __str__(self): return f"{self.amount} in Listing No: {self.auction.id}" class Meta: ordering = ['amount'] @staticmethod def high_bid(auction, bidder, bid_amount): """util method to ascertain highest bid in auction then create in related auction obj :param auction---listing being bid on, bid__auction :param bidder---user bidding :param amount--- current highest bid """ ###error checks, is auction running? is current bid less than start bid? etc if bid_amount < auction.start_bid: return if (auction.highest_offer and bid_amount < auction.highest_offer.amount): return if bidder.id is auction.user.id: raise PermissionDenied ##after checks create highest bid object in listing model new_high_bid = Bid.objects.create( auction= auction, bidder = bidder, amount = bid_amount )
1 回答
开满天机
TA贡献1786条经验 获得超13个赞
从 form_valid 中删除该return super().form_valid(form)
行。
这行代码将从继承类中调用 form_valid 来保存对象 - 这是在没有外键的情况下创建的额外对象。
相反,您将希望返回某种 http 响应(例如 render_to_response、重定向等)。
添加回答
举报
0/150
提交
取消