This is my Auction model.

class Auction(models.Model):
    auction_id = models.AutoField(primary_key=True)
    crop = models.ForeignKey(Crop, on_delete=models.CASCADE)
    image = models.ImageField(upload_to="crop-image")
    farmer = models.ForeignKey(User, on_delete=models.CASCADE, limit_choices_to={'user_type': 'farmer'})
    creation_time = models.DateTimeField(auto_now_add=True)
    end_date = models.DateField()
    end_time = models.TimeField()
    status = models.BooleanField(default=True)
    qty = models.IntegerField()
    unit = models.CharField(max_length=10, choices=[('kg', 'Kilograms'), ('tonne', 'Metric Tons'), ('bushel', 'Bushels'), ('crate', 'Crates')])
    hammer_price = models.IntegerField()
    description = models.CharField(max_length=200)
    payment = models.BooleanField(default=False)

    class Meta:
        verbose_name_plural = "Auctions"

    def __str__(self):
        return self.crop.title

When combined end_date and end_time is reached, status which is by default set to 1 on auction creation should be changed to 0.

推荐答案

我会简单地添加一个状态,但判断是否使用了结束时间戳.通过定义一个字段status,你引入了data duplication,你指定了同样的东西twice.

您可能还应该combine日期和时间,因为时间戳不仅仅是日期和时间,例如daylight saving time.

from django.conf import settings


class Auction(models.Model):
    auction_id = models.AutoField(primary_key=True)
    crop = models.ForeignKey(Crop, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='crop-image')
    farmer = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        limit_choices_to={'user_type': 'farmer'},
    )
    creation_time = models.DateTimeField(auto_now_add=True)
    end_timestamp = models.DateTimeField()
    # no status
    qty = models.IntegerField()
    unit = models.CharField(
        max_length=10,
        choices=[
            ('kg', 'Kilograms'),
            ('tonne', 'Metric Tons'),
            ('bushel', 'Bushels'),
            ('crate', 'Crates'),
        ],
    )
    hammer_price = models.IntegerField()
    description = models.CharField(max_length=200)
    payment = models.BooleanField(default=False)

    class Meta:
        verbose_name_plural = 'Auctions'

    def __str__(self):
        return self.crop.title

例如,我们可以获得所有仍然活跃的Auction:

from django.db.models.functions import Now

Auction.objects.filter(end_timestamp__gte=Now())

现在,我们以passive种方式确定状态,这通常比主动改变状态的进程更可靠.

我们可以通过设置db_index=True [Django-doc]来提高性能.


Note:通常使用settings.AUTH_USER_MODEL [Django-doc]来引用用户模型比直接使用User model [Django-doc]更好.更多信息可以看referencing the User model section of the documentation [Django-doc].

Python相关问答推荐

如何通过多2多字段过滤查询集

Polars比较了两个预设-有没有方法在第一次不匹配时立即失败

如何在箱形图中添加绘制线的传奇?

Gekko:Spring-Mass系统的参数识别

. str.替换pandas.series的方法未按预期工作

如何从具有不同len的列表字典中创建摘要表?

Mistral模型为不同的输入文本生成相同的嵌入

所有列的滚动标准差,忽略NaN

有没有一种ONE—LINER的方法给一个框架的每一行一个由整数和字符串组成的唯一id?

如何并行化/加速并行numba代码?

Python导入某些库时非法指令(核心转储)(beautifulsoup4."" yfinance)

使用Python从URL下载Excel文件

名为__main__. py的Python模块在导入时不运行'

如何排除prefecture_related中查询集为空的实例?

在Docker容器(Alpine)上运行的Python应用程序中读取. accdb数据库

Discord.py -

如何防止html代码出现在quarto gfm报告中的pandas表之上

当我定义一个继承的类时,我可以避免使用`metaclass=`吗?

如何在Polars中创建条件增量列?

VSCode Pylance假阳性(?)对ImportError的react