django: inlineformset very slow

Viewed 157

I have the following models:

class Recipe(models.Model):  
   ....

class Ingredient(models.Model):  
   ....

class RecipePosition(models.Model):  
   recipe = models.ForeignKey(Recipe,related_name='recipe_positions', on_delete=models.CASCADE)  
   ingredient = models.ForeignKey(Ingredient,related_name='ingredient_recipeposition',on_delete=models.PROTECT) ....

in my views.py i am trying to create an inlineformset so that i can edit all the Reciposition related to particular Recipe:

def recipe_ingredients_formset_update(request,slug=None):
    instance = get_object_or_404(Recipe.objects.prefetch_related('recipe_positions__ingredient'), slug=slug)
    RecipeIngredientsFormSet = inlineformset_factory(Recipe,RecipePosition,form=RecipePoistionForm,  can_delete=True, extra=5)
    if request.method == "POST":
        formset = RecipeIngredientsFormSet(request.POST, request.FILES, instance=instance)
        helper = RecipePositionCreateFormSetHelper()
        if formset.is_valid():
            formset.save()
            # Do something. Should generally end with a redirect. For example:
            messages.success(request, "Successfully Updated", extra_tags='alert')
            return HttpResponseRedirect('')
    else:
        formset = RecipeIngredientsFormSet(instance=instance)
        helper = RecipePositionCreateFormSetHelper()

    context = {
        "instance":instance,
        "formset":formset,
        "helper":helper,
        "url":instance.get_absolute_url_recipe_update_inline_bulk_ingredients()
        }

    return render(request, 'recipe_recipositions_bulk_edit.html', context)

I searched on net, but not able to understand. I am using Django Debug toolbar.

If i have 56 RecipePosition items for a particular Recipe. it took me 36 seconds to load

0 Answers
Related