模型排序法

2024-04-19 20:32:38 发布

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

我有一个开放时间表,我需要根据“开放时间”对项目进行排序,因此它将显示“现在开放”,然后显示“现在关闭”项目。但我不能用模型方法过滤

型号.py

class Company(ImageBaseModel):
    class Meta:
        verbose_name = _(u"Empresa")
        verbose_name_plural = _(u"Empresas")
        ordering = ('order',)
    user                            = ForeignKey('auth.User', verbose_name=_(u"Responsável"), null=True, blank=True)

    # Admin fields
    slug            = SlugField(verbose_name=_(u'Slug / URL'), blank=True, null=True)
    order           = PositiveIntegerField(verbose_name=_(u"Ordem"), null=True, blank=True)

    def __unicode__(self):return u'%s'%self.title
    def get_address(self):
        if self.cep and self.address and self.city and self.number: return True

    def isopen(self):
        now = datetime.now()
        nowTime = time(now.hour, now.minute, now.second)
        ohs = self.opening_times.filter(company__slug=self.slug)
        status = False
        for oh in ohs:
            is_open = False
            # start and end is on the same day
            if oh.weekday == now.isoweekday() and oh.fromHour <= nowTime and nowTime <= oh.toHour:
               is_open = oh

            # start and end are not on the same day and we test on the start day
            if oh.weekday == now.isoweekday() and oh.fromHour <= nowTime and ((oh.toHour < oh.fromHour) and (nowTime < datetime.time(23, 59, 59))):
                is_open = oh

            # start and end are not on the same day and we test on the end day
            if (oh.weekday == (now.isoweekday()-1)%7 and oh.fromHour >= nowTime and oh.toHour >= nowTime and oh.toHour < oh.fromHour):
                is_open = oh
                #print " 'Special' case after midnight", oh

            if is_open is not False:
                status = True
        return status
    def hasOt(self): #have opening times registry
        return len(self.opening_times.all()[:1]) == 1

class OpeningHours(Model):
    class Meta:
        verbose_name = _(u"Horário de Abertura")
        verbose_name_plural = _(u"Horários de Abertura")
        unique_together = ('company', 'weekday')
    company = ForeignKey(Company, related_name="opening_times", verbose_name=_(u"Empresa"))
    weekday = IntegerField(choices=WEEKDAYS, verbose_name=_(u"Dia da Semana"))
    fromHour = TimeField(verbose_name=_(u"Abre ás:"), null=True, blank=True)
    toHour = TimeField(verbose_name=_(u"Fecha ás:"), null=True, blank=True)

    def __unicode__(self):
        return "%s %s (%s - %s)" % (self.company, self.weekday, self.fromHour, self.toHour)

视图.py

def home(request):
    companies       = Company.objects.filter(published=True).order_by('opening_times')
    food_companies  = companies.all()[0:5] #here I'll have to filter only food companies open now. 

我需要一些像食品公司的东西。按('先打开')订购,但我不知道怎么做


Tags: andnameselftrueverboseisdefopen
1条回答
网友
1楼 · 发布于 2024-04-19 20:32:38

order_by应用于数据库级别(即在原始SQL中),因此不能用它调用函数。不过,您可以获取所有的food_companies,并使用isopen作为键对它们进行排序。比如:

sorted_companies = sorted(food_companies.objects.all(),
                          key=lambda food_company: food_company.isopen())

相关问题 更多 >