-1
我試圖按相關字段上的多個值進行排序,按鍵過濾,但運行第二個order_by
按排序,第一個過濾器,而不是最近的。Django按多個過濾的外鍵值排序
class Product(Model):
name = CharField()
class ProductAttribute(Model):
product = ForeignKey(Product)
key = CharField()
value = FloatField()
# This line sorts products as expected over the `ProductAttribute`s with a key of 'score'
products = Product.objects.filter(productattribute_set__key='score')\
.order_by('productattribute_set__value')
# This line sorts products as expected over the `ProductAttribute`s with a key of 'rating'
products = Product.objects.filter(productattribute_set__key='rating')\
.order_by('productattribute_set__value')
# This line sorts products based on score only, not on rating, then score (or vise versa)
products = Product.objects.filter(productattribute_set__key='score')\
.order_by('productattribute_set__value')\
.filter(productattribute_set__key='rating')\
.order_by('productattribute_set__value')
有沒有辦法通過score
訂購的價值,然後rating
的價值?
不幸的是,這不是一個真正的選擇。一個Product可以有30多種不同的'ProductAttribute',其中沒有一個是保證存在的。在我們的例子中'Product'是父項,'ProductAttribute'是子項。 – TorelTwiddler
你的問題不是很清楚@TorelTwiddler這似乎是一個合法的答案根據我對你的問題的理解 – e4c5
@ e4c5我不知道我怎麼能使它更清楚。這不是一個可以接受的答案,因爲我需要做出非常大量的更改(工作周)才能進行此更改。我不覺得說「改變你的前提」是解決問題的好辦法。 – TorelTwiddler