我正在尝试使用拍卖行风格模型的一些概念,现在我有一个Auction模型和一个Bid模型。两者通过ForeignKey在Bid模型中关联,Bid.amount包含用户出价的金额。
我已经使用Bid.amount字段的顺序来定义最高出价,但我想知道是否有一种简单的方法来定义max_bid,而用户的输出看起来像是一个“智能投标”系统,一个la 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,因为没有其他出价。
当bid2被放置时,当前的拍卖价格应该会上升到6001 (因为bid1仍然更高)。
当bid3被放置时,目前的拍卖价格应该提高到9001 (出价超过bid1,成为目前出价最高的)。
如果有人想出最好的办法来解决这个问题,我很想听听他们的意见。
编辑:我的模型,供参考
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'发布于 2014-12-23 03:29:28
我将为您的拍卖模型创建一个函数,并使用@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 }}https://stackoverflow.com/questions/27611521
复制相似问题