使用自定义方法的Django order by Manytomy字段

2024-04-25 05:52:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我怎样才能按折扣价订购所有服务折扣.u是否有效()? 你知道吗

你知道吗型号.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')

所以在服务中,我有按折扣的最大折扣订购的所有服务(与每个服务相关),但我要按折扣值订购的所有服务,其中折扣.u是否有效()? 你知道吗

那么,我怎样才能得到所有的服务按折扣值订购在哪里折扣.u是否有效()? 你知道吗

任何帮助都将不胜感激:)


Tags: tofromselftrueismodelsservicediscount