gpt4 book ai didi

python - 创建 eBay 最高出价风格的拍卖模型

转载 作者:太空宇宙 更新时间:2023-11-03 18:01:32 25 4
gpt4 key购买 nike

我正在尝试使用拍卖行风格模型的一些概念,现在我有一个 Auction 模型和一个 Bid 模型。两者通过 Bid 模型中的外键关联,Bid.amount 包含用户出价的金额。

我已经利用 Bid.amount 字段上的排序来定义最高出价,但我想知道是否有一种简单的方法来定义 max_bid 和向用户输出的内容看起来像是一个“智能出价”系统,类似于 eBay。

那么请说明以下情况是否适用

$ bid1 = 9000  # max bid for bid1 object
$ bid2 = 6000 # max bid for bid2 object
$ bid3 = 9500 # max bid for bid3 object
$ starting_price = 5000 # starting price for the auction

当发出 bid1 时(表明智能出价应达到的最高价为 9000),当前拍卖价格应保持在 5000,因为该商品没有其他出价。

当出价 2 被放置时,当前拍卖价格应增加到 6001(因为出价 1 仍然更高)

当出价 3 后,当前拍卖价格应增加到 9001(出价高于出价 1,并成为当前最高出价者)。

如果有人对解决这个问题的最佳方法有任何想法,我很想听听。

编辑:我的模型,供引用

class Auction(models.Model):
seller = models.ForeignKey(User)
item_id = models.CharField(max_length=255, blank=True, null=True)
item_name = models.CharField(max_length=255, blank=True, null=True)
winner = models.ForeignKey(User, related_name='Auction_Winner', blank=True, null=True)
reserve = models.CharField(max_length=255, blank=True, null=True)
is_guildbank_sale = models.BooleanField(default=True)
created = models.DateTimeField(editable=False, null=True)
expires = models.DateTimeField(editable=False, null=True)

def __unicode__(self):
return '%s selling %s' % (self.seller, self.item_name)

def save(self, *args, **kwargs):
''' On save, update timestamps '''
if not self.id:
self.created = datetime.today()
self.expires = datetime.today() + timedelta(days=3)
return super(Auction, self).save(*args, **kwargs)


class Bid(models.Model):
auction = models.ForeignKey(Auction, null=True)
user = models.ForeignKey(User, related_name='bid_owner', null=True)
bid_amount = models.IntegerField(blank=True, null=True)

class Meta:
verbose_name = "Auction Bid"
verbose_name_plural = "Auction Bids"
ordering = ['-bid_amount',]
get_latest_by = 'bid_amount'

最佳答案

我将为您的拍卖模型创建一个函数,并使用 @property 装饰器将其设为实例属性。我不会将其存储为数据库值,因为您将遇到竞争条件问题。

class Auction(models.Model):
...
def _get_increment(self):
""" add some logic to base incrementing amount on starting price """
...

@property
def current_auction_price(self):
price = self.starting_amount
bid_increment = self._get_increment()

""" If there is more than 1 bid present, take the second highest value and add the bid increment. """
if self.bid_set.count() > 1:
price = self.bid_set.order_by('bid_amount')[-2].bid_amount + bid_increment

return price

这将允许您直接在模板中使用该值。

{{ object.current_auction_price }}

关于python - 创建 eBay 最高出价风格的拍卖模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27611521/

25 4 0
文章推荐: c# - 在 Visual Studio 2008 中将项目更改为 Web 应用程序
文章推荐: jquery - 单击链接标记时在
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com