我正在使用 Django createview 在拍卖网站中创建投标项目。createview 将创建新对象,但它会创建一个没有相应外键的额外对象实例。我正在使用 @staticmethod 来确定提交的出价是否确实是最高出价,然后在相关列表中创建。如果您能指出我做错了什么,请提前致谢。
models.py
class 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
)
开满天机
相关分类