Python Django order by Manytomy字段使用自定义方法
如何获取按折扣值订购的所有服务,其中折扣处于活动状态() models.py 因此,在服务中,我将所有服务按所有折扣的最大折扣(与每项服务相关)订购,但我希望所有服务按折扣值订购,其中折扣。是否为活动() 那么,我如何才能获得按折扣值订购的所有服务,其中折扣是活动的()Python Django order by Manytomy字段使用自定义方法,python,django,django-models,Python,Django,Django Models,如何获取按折扣值订购的所有服务,其中折扣处于活动状态() models.py 因此,在服务中,我将所有服务按所有折扣的最大折扣(与每项服务相关)订购,但我希望所有服务按折扣值订购,其中折扣。是否为活动() 那么,我如何才能获得按折扣值订购的所有服务,其中折扣是活动的() 任何帮助都将不胜感激:)您可以声明作为@属性处于活动状态并按折扣进行筛选\u处于活动状态=True我不想进行筛选。我需要所有的服务。。。但在所有服务中,我想按折扣值订购…只使用有效的折扣。。。(所以,如果我能在第二阶段应用过滤器
任何帮助都将不胜感激:)您可以声明作为
@属性处于活动状态
并按折扣进行筛选\u处于活动状态=True
我不想进行筛选。我需要所有的服务。。。但在所有服务中,我想按折扣值订购…只使用有效的折扣。。。(所以,如果我能在第二阶段应用过滤器,那就太好了……但我不知道怎么做)
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')