2017-08-01 60 views
0

我在Django的Admin界面& Postgres中遇到了一些性能問題。我已經將其縮小到針對RecipeControl模型中的每個IngredientInline執行的查詢。配方中存在的成分越多,頁面加載的時間越長,因爲似乎要多次爲IngredientInline加載查詢集(近2000條記錄)。在Django ManyToMany中減少Postgresql查詢TabularAdminInline

我確信解決方案是在加載頁面之前預先緩存查詢集,但我很困惑這是如何工作的,不知道這會導致什麼樣的問題。我研究了prefetch_related vs select_related之間的區別,並試圖使用兩者,但在執行任何操作時,似乎沒有任何性能變化。我也found this question但我使用管理界面,不寫我自己的看法。那麼,我如何正確覆蓋哪個管理模塊以產生所需的效果?謝謝你的幫助。

我有一個模型,如下所示:

class RecipeControl(models.Model): 
    #recipe_name choice field needs to be a query set of all records containing "FG-Finished Goods" 
    recipe_name = models.ForeignKey(items.IngredientList, related_name='recipe_name', limit_choices_to={'category': 'FG'}) 
    customer_recipe = models.ForeignKey(custmods.CustomerProfile, verbose_name='customer', related_name='customer_recipe') 
    ingredients = models.ManyToManyField(items.IngredientList, through='RecipeIngredients') 
    active_recipe = models.BooleanField(default=False) 
    active_by = models.CharField(max_length=64, editable=False) 
    revision = models.IntegerField(default=0) 
    last_updated = models.DateTimeField(auto_now_add=True, editable=False) 
    mixer_volume = models.DecimalField(verbose_name='Mixer Volume(L)', max_digits=16,decimal_places=3, blank=True, null=True) 
    fill_factor = models.DecimalField(verbose_name='Fill %', max_digits=6,decimal_places=2,blank=True,null=True) 
    def __str__(self): 
     return "%s" % (self.recipe_name) 

class RecipeIngredients(models.Model): 
    recipe = models.ForeignKey(RecipeControl, related_name='recipe') 
    ingredient = models.ForeignKey(items.IngredientList, related_name='ingredient') 
    weight_tolerance = models.DecimalField(verbose_name="PPH Tolerance",max_digits=8, decimal_places=3, blank=True, null=False) 
    recipe_weight = models.DecimalField(verbose_name="PPH",max_digits=16, decimal_places=3, blank=True, null=True) 

    def __str__(self): 
     return "%s" % (self.ingredient) 

我的admin.py文件:

class IngredientInline(admin.TabularInline): 
    model = RecipeIngredients 
    #prefetch_related = ('ingredient',) 
    readonly_fields = ('percentage', 'item_price','ext_price','SPG') 
    fieldsets = [(None,{'fields':[('ingredient','item_price','ext_price','SPG','percentage','weight_tolerance','recipe_weight')]})] 
    extra = 0 
    template = 'admin/recipes/recipeingredients/edit_inline/tabular.html' 

class RecipeView(admin.ModelAdmin): 
    def save_model(self, request, obj, form, change): 
     obj.active_by = request.user.username 
     obj.save() 

    list_select_related = ['recipe_name', 'customer_recipe'] 
    list_display = ['recipe_name','customer_recipe','active_recipe','last_updated','active_by'] 
    list_display_links = ['recipe_name'] 
    list_filter = ['active_recipe'] 
    search_fields = ['recipe_name__name', 'recipe_name__item_code','customer_recipe__name'] 
    readonly_fields = ('last_updated','active_by','batch_weight','calculated_batch', 'recipe_gravity') 

    fieldsets = [ 
     ('Recipe Information',{'fields': [('recipe_name','customer_recipe','active_recipe')]}), 
     ('Audit Trail', {'fields': [('active_by','revision','last_updated')]}), 
     ('Batch Weight Info',{'fields': [('batch_weight', 'mixer_volume', 'fill_factor','recipe_gravity', 'calculated_batch')]}) 
    ] 
    inlines = [IngredientInline] 

回答

0

用戶界面是不理想,但最快的解決辦法是使用raw_id_fields與任何外鍵很多可能性。

class IngredientInline(admin.TabularInline): 
    model = RecipeIngredients 
    raw_id_fields = ['ingredients'] 

如果你需要一個更好的用戶界面,你可以尋找一個外部的軟件包,如django-select2

+0

這是可能的解決方案= D希望用戶同意!我下週爲他們做一個小型的演示,並由他們運行。感謝您的輸入! – Dan2theR