Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建一个拍卖模型,采用易趣最高出价风格_Python_Django_Django Models - Fatal编程技术网

Python 创建一个拍卖模型,采用易趣最高出价风格

Python 创建一个拍卖模型,采用易趣最高出价风格,python,django,django-models,Python,Django,Django Models,我正在尝试一些拍卖行风格模型的概念,现在我有一个拍卖模型和一个出价模型。这两者通过Bid模型中的外键关联,其中Bid.amount包含用户已出价的金额 我已经在Bid.amount字段中使用了排序来定义最高出价,但我想知道是否有一种简单的方法来定义max_Bid,并且向用户输出的是看起来像“智能竞价”系统的东西,即易趣 因此,如果以下情况适用,请说明 $ bid1 = 9000 # max bid for bid1 object $ bid2 = 6000 # max bid for bid

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

我已经在
Bid.amount
字段中使用了排序来定义最高出价,但我想知道是否有一种简单的方法来定义
max_Bid
,并且向用户输出的是看起来像“智能竞价”系统的东西,即易趣

因此,如果以下情况适用,请说明

$ 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 decorator将其作为实例属性。我不会将其存储为数据库值,因为您将遇到竞争条件问题

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_集合没有属性,我相信您要查找的是。order_by。既然这已经在Meta类中定义了,那么它不需要是.all()吗?是的,负索引是有效的。[链接](www.diveintopython.net/native_data_types/lists.html)。我的错误是从下到下排序,从下到下排序。我将排序改为升序,并通过方法名修正了顺序。很好,这解释了为什么我会出现负索引错误。谢谢你澄清这一点。:)我错过了你们的元类排序,所以你们不需要像我一样重新排序。@VictorBruno在github上的django项目中有这个例子的源代码吗?