我目前正在研究一個Django庫來管理多個分辨率的圖像(django-multires),我堅持優化保留關係查詢。在我解釋這個問題之前,讓我試着解釋我想達到的目標。Django查詢沒有外鍵的反向關係
背景
的想法是存儲圖像的多種分辨率然而保持圖像路徑,而不是外鍵方面對原始圖像的引用。我認爲一個例子會更有意義:
# the goal is to store multiple resolutions for 'image' field
class FooModel(models.Model):
image = MultiresImageField(...)
# MultiresImage.source will be the identical to FooModel.image
# so MultiresImage.source will act sort of like a foreign key
class MultiresImage(models.Model):
source = models.ImageField(...)
...
使用這種方法,而不是使用一個外鍵(或通用外鍵)鏈接到源圖像允許多個MultiresImageField
s添加到源模型:
class FooModel(models.Model):
image = MultiresImageField(...)
image2 = MultiresImageField(...)
現在讓我們說,你需要得到源模型中對圖像領域的所有不同的分辨率:
foo = FooModel(...)
foo.image.get_all_multires_images()
# which behind the scenes will do something similar to
return MultiresImage.objects.filter(source=foo.image.name)
直到你需要牛逼效果很好Ø查詢多個FooModel
情況下,在這種情況下,每個模型實例,我不得不做一個數據庫查詢,以獲取該模型的所有決議:
sources = FooModel.objects.filter(...)
for source in sources:
# this incurs a db query
foo.image.get_all_multires_images()
通常我會用prefetch_related
但是做性能優化在這裏我可以因爲我的multires模型沒有源模型的外鍵,因此源模型中不存在反向關係。
問題
所以我的問題是如何能在上面的查詢進行優化?
目前的一些想法
- 因爲我想提出一個自定義模型領域,我可以用
contribute_to_class
手動添加到源模型的反向關係,但我想不通怎麼樣? - 另一個雖然是使用
Prefetch
API Django>=1.7但我不知道如何使這項工作以及。
沒有關於您的自定義MuliResImageField的更多信息,很難給您適當的建議。無論如何我不明白的是:爲什麼你不能使用簡單的ForeignKey。例如。 image = models.ForeignKey(MultiResImage);圖像2 = models.ForeignKey(MultiResImage)。 – schacki
我想'MultiresImageField'與常規Django圖像字段向後兼容。您可以在github上看到'MultiresImageField'的當前代碼,但是我不認爲這有助於解決這個查詢問題。 – miki725