【问题标题】:Creating an auction model a la eBay max bid style以 eBay 最高出价风格创建拍卖模型
【发布时间】:2014-12-24 02:19:49
【问题描述】:

我正在尝试使用拍卖行风格模型的一些概念,现在我有一个Auction 模型和一个Bid 模型。两者通过 Bid 模型中的 ForeignKey 关联,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

当出价 1 被放置时(表明智能出价应达到的最高价为 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'

【问题讨论】:

    标签: python django django-models


    【解决方案1】:

    我将为您的 Auction 模型创建一个函数,并使用 @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 }}
    

    【讨论】:

    • 感谢您,但是我发现您可能希望在代码中更正一些错误。首先,您不能有负指数,因此获得第二高的出价将是 [1] 而不是 [-2]。其次,没有名为 order 的bid_set 的属性,我相信您要查找的是.order_by。既然这已经在 Meta 类中定义了,这不是只需要 .all() 吗?
    • 是的,负索引是有效的。 [链接](www.diveintopython.net/native_data_types/lists.html)。我的错误是按降序排序,但仍然从最后开始。我将排序更改为升序并修复了 order_by 方法名称。
    • 完美,这解释了为什么我得到负索引错误。感谢您清除它。 :)
    • 我想念你的元类排序,所以你不需要像我一样重新排序。
    • @VictorBruno 在 github 上的 django 项目中是否有此示例的源代码?
    猜你喜欢
    • 2015-07-24
    • 1970-01-01
    • 2011-12-22
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多