我正在尝试根据促销进行一些复杂的排序:自创建以来每 7 天促销一篇文章(文章在 30 天后过期)。
我的方法是用自创建以来的天数来注释查询集,但days_since_creation在我的代码中,注释字段 ( )的值始终为 0。
from datetime import timedelta
from django.test import TestCase
from django.db.models import ExpressionWrapper, F
from django.db.models.fields import IntegerField
from django.utils import timezone
from .models import Article
# class Article(models.Model):
# title = models.CharField(max_length=150)
# creation_date = models.DateTimeField()
#
# def __str__(self):
# return self.title
class ArticleTestCase(TestCase):
def test_days_since_creation(self):
now = timezone.now()
objects_data = [
Article(
title='Alrp',
creation_date=(now - timedelta(days=5)) # 5 days ago
),
Article(
title='Bopp',
creation_date=(now - timedelta(days=7)) # …Run Code Online (Sandbox Code Playgroud)