按公式排序

时间:2017-03-14 21:31:11

标签: python django

我正在过滤商店。我需要通过评级对商店进行排序,但是评级是通过复杂的形式来考虑的,我可以按一个字段对它们进行排序,但是如何计算每个我不知道的数量

这是按费率排列的当前版本

shops = shops.order_by('rate')

但你需要把公式代替场率,这是怎么做的?

class Shop(models.Model):
name = models.CharField(max_length=255, verbose_name=u'Название')
time_begin = models.TimeField(max_length=255,
                              verbose_name=u'Время начала работы')
time_end = models.TimeField(max_length=255,
                            verbose_name=u'Время окончания работы')
phone = models.CharField(max_length=255, verbose_name=u'Телефон')
preview = models.FileField(upload_to='files/shop/preview')
delivery_price = models.IntegerField(verbose_name=u'Стоимость доставки')
min_price = models.IntegerField(verbose_name=u'Мин доставка', null=True)
min_order = models.IntegerField(verbose_name=u'Минимальный заказ')
del_wait_time = models.IntegerField(verbose_name=u'Время доставки минут')
is_always = models.BooleanField(verbose_name=u'Круглосуточно?',
                                default=True, blank=True)
is_cash = models.BooleanField(verbose_name=u'Наличными',
                              default=True, blank=True)
is_card = models.BooleanField(verbose_name=u'Картой курьеру',
                              default=True, blank=True)
is_online = models.BooleanField(verbose_name=u'Онлайн-оплата',
                                default=True, blank=True)
is_points = models.BooleanField(verbose_name=u'Еда за баллы',
                                default=True, blank=True)
is_sale = models.BooleanField(verbose_name=u'Акция',
                                default=True, blank=True)
is_new = models.BooleanField(verbose_name=u'Новый магазин',
                                default=True, blank=True)
notification = models.CharField(max_length=255,
                                verbose_name=u'Важное уведомление',
                                blank=True, null=True)
email = models.CharField(max_length=255, verbose_name=u'E-mail')
review_email = models.CharField(max_length=255,
                                verbose_name=u'Review E-mail',
                                default='null')
rate = models.FloatField(max_length=255, verbose_name=u'Рейтинг')
destination = models.CharField(max_length=255,
                               verbose_name=u'Местоположение')
specific = models.CharField(max_length=255, default='',
                            verbose_name=u'Специализация ресторана')
description = models.TextField(max_length=3000,
                               verbose_name=u'Описание')
cook = models.CharField(max_length=255, verbose_name=u'Имя повара',
                        blank=True, null=True)
image_cook = models.FileField(upload_to='files/shop/cook',
                              blank=True, null=True)
desc_cook = models.TextField(max_length=3000,
                             verbose_name=u'Описание повара',
                             blank=True, null=True)
shoptype = models.ForeignKey(ShopType, verbose_name=u'Тип заведения',
                             related_name="shop")
slider = models.ManyToManyField(Slider, verbose_name=u'Слайдер',
                                related_name="slider",
                                blank=True)
kitchen = models.ManyToManyField(Kitchen)

1 个答案:

答案 0 :(得分:1)

缺乏对问题的更详细陈述,我会给出一个模糊的答案。 (如果有更多细节显示,则填写)

您可以通过使用计算值注释查询集来执行此操作。请参阅文档here

这意味着您将要求数据库计算出“费率”,例如将每个商店的距离除以时间,并将该值添加到每个商店返回的数据中。然后,您可以根据该字段执行order_by。