我的应用程序中有一个搜索功能,它接收“城市”和“持续时间”输入(两个列表)并返回按包“评级”排序的前 30 个匹配的“包”结果。
如果所有参数都是列,则很容易实现,但“持续时间”和“评级”是计算属性。这意味着我不能使用标准的 Django 查询来过滤包。看来我需要在这里使用Django 的“额外”方法,但我的 SQL 不是很好,这似乎是一个非常复杂的查询。
我应该在这里使用额外的方法吗?如果是这样,该声明会是什么样子?
适用的代码复制如下。
#models.py
class City(models.Model):
...
city = models.CharField(max_length = 100)
class Package(models.Model):
....
city = models.ManyToManyField(City, through = 'PackageCity')
@property
def duration(self):
duration = len(Itinerary.objects.filter(package = self))
return duration
@property
def rating(self):
#do something to get the rating
return unicode(rating)
class PackageCity(models.Model):
package = models.ForeignKey(Package)
city = models.ForeignKey(City)
class Itinerary(models.Model):
# An Itinerary object is a day in a package, so len(Itinerary) works for the duration
...
package = models.ForeignKey(Package)
#functions.py
def get_packages(city, duration):
cities = City.objects.filter(city = city) # works fine
duration_list = range(int(duration_array[0], 10), int(duration_array[1], 10) + 1) # works fine
#What I want to do, but can't because duration & rating are calculated properties
packages = Package.objects.filter(city__in = cities, duration__in = duration_array).order_by('rating')[:30]