2017-08-01 4 views
0

Djangoの管理インターフェイス& Postgresでパフォーマンスの問題が発生しています。レシピコントロールモデルの各IngredientInlineで実行されたクエリに絞り込んだ。レシピ内に存在する成分が多いほど、IngredientInlineのクエリーセット(約2,000レコード)を複数回ロードする必要があるため、ページを読み込む時間が長くなります。DjangoのPostgresqlクエリを減らすManyToMany TabularAdminInline

私は、ページをロードする前にクエリーセットをプリキャッシュしておくことが解決策であると確信していますが、どのように動作しているのか分かりません。私はprefetch_related vs select_relatedの違いを調べて、両方を使用しようとしましたが、どちらかを実行するときにパフォーマンスに変化がないようです。私もfound this questionしかし、私は自分のビューを書いていない管理インタフェースを使用しています。それで、どのように/どのadminモジュールを適切にオーバーライドして、望ましい効果を生み出すのですか?助けてくれてありがとう。

次のように私はモデルを持っている:

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

UIは理想的ではありませんが、最速の修正が持つすべての外部キーのためraw_id_fieldsを使用することです多くの可能性。あなたがよりよいUIが必要な場合は

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

、あなたはdjango-select2として、外部のパッケージのために、このようなを見ることができます。

+0

これは可能な解決策です。= Dうまくいけば、ユーザーは同意します!私は来週、彼らのために小さなプレゼンテーションを行い、彼らがそれを実行します。入力いただきありがとうございます! – Dan2theR

関連する問題