如何在 discount.is_active() 的位置获得按 discount__value 排序的所有服务?
模型.py
class Service(models.Model):
name = models.CharField(max_length=50)
length = models.IntegerField()
description = models.CharField(max_length=150, null=True, blank=True)
long_description = models.TextField(null=True, blank=True)
price = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
provider = models.ForeignKey(settings.AUTH_USER_MODEL)
categories = models.ManyToManyField(Category)
active_from = models.DateField(null=True, blank=True)
active_to = models.DateField(null=True, blank=True)
rating = models.FloatField(null=True, blank=True)
review_number = models.IntegerField(null=True, blank=True)
discount = models.ManyToManyField(Discounts, null=True, blank=True)
class Discounts(models.Model):
value = models.IntegerField(blank=True, default=0)
active_from = models.DateField(null=True, blank=True)
active_to = models.DateField(null=True, blank=True)
def is_active(self):
return (self.active_from is None or self.active_from <= date.today()) and \
(self.active_to is None or self.active_to >= date.today())
在我看来我有
filtered_services = Service.objects.filter(not important)
services = filtered_services.annotate(highest_discount=Max('discount__value')).order_by('-highest_discount')
因此,在服务中,我按所有折扣(与每项服务相关)的最大折扣订购了所有服务,但我希望所有服务按 discount__value 订购,其中 discount.is_active()?
那么如何在 discount.is_active() 的位置获得按 discount__value 排序的所有服务?
任何帮助将非常感激 :)