# Buggy code
def category_delete(request, pk):
    category = get_object_or_404(Category, pk=pk)
    if request.method == "POST":
        descendants = category.get_descendants()
        menus = get_menus_that_needs_update(categories=descendants)
        category.delete()
        if menus:
            update_menus(menus)
        messages.success(
            request,
            pgettext_lazy("Dashboard message", "Removed category %s") % category,
        )
        root_pk = None
        if category.parent:
            root_pk = category.parent.pk
        if root_pk:
            if request.is_ajax():
                response = {
                    "redirectUrl": reverse(
                        "dashboard:category-details", kwargs={"pk": root_pk}
                    )
                }
                return JsonResponse(response)
            return redirect("dashboard:category-details", pk=root_pk)
        else:
            if request.is_ajax():
                response = {"redirectUrl": reverse("dashboard:category-list")}
                return JsonResponse(response)
            return redirect("dashboard:category-list")
    ctx = {
        "category": category,
        "descendants": list(category.get_descendants()),
        "products_count": len(category.products.all()),
    }
    return TemplateResponse(
        request, "dashboard/category/modal/confirm_delete.html", ctx
    )


# Traceback (ValueError)
Traceback (most recent call last):
File "/Users/mikail/Development/saleor-venv/lib/python3.7/site-packages/promise/promise.py", line 487, in _resolve_from_executor
executor(resolve, reject)
File "/Users/mikail/Development/saleor-venv/lib/python3.7/site-packages/promise/promise.py", line 754, in executor
return resolve(f(*args, **kwargs))
File "/Users/mikail/Development/saleor-venv/lib/python3.7/site-packages/graphql/execution/middleware.py", line 75, in make_it_promise
return next(*args, **kwargs)
File "/Users/mikail/Development/saleor/saleor/graphql/core/mutations.py", line 207, in mutate
response = cls.perform_mutation(root, info, **data)
File "/Users/mikail/Development/saleor/saleor/graphql/core/mutations.py", line 354, in perform_mutation
instance = cls.construct_instance(instance, cleaned_input)
File "/Users/mikail/Development/saleor/saleor/graphql/core/mutations.py", line 184, in construct_instance
f.save_form_data(instance, data)
File "/Users/mikail/Development/saleor-venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 853, in save_form_data
setattr(instance, self.name, data)
File "/Users/mikail/Development/saleor-venv/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 211, in __set__
self.field.remote_field.model._meta.object_name,
ValueError: Cannot assign "<Page: About>": "MenuItem.category" must be a "Category" instance.

# Fixed code
def category_delete(request, pk):
    category = get_object_or_404(Category, pk=pk)
    if request.method == "POST":
        descendants = category.get_descendants()
        menus = get_menus_that_need_update(categories=descendants)
        category.delete()
        if menus:
            update_menus(menus)
        messages.success(
            request,
            pgettext_lazy("Dashboard message", "Removed category %s") % category,
        )
        root_pk = None
        if category.parent:
            root_pk = category.parent.pk
        if root_pk:
            if request.is_ajax():
                response = {
                    "redirectUrl": reverse(
                        "dashboard:category-details", kwargs={"pk": root_pk}
                    )
                }
                return JsonResponse(response)
            return redirect("dashboard:category-details", pk=root_pk)
        else:
            if request.is_ajax():
                response = {"redirectUrl": reverse("dashboard:category-list")}
                return JsonResponse(response)
            return redirect("dashboard:category-list")
    ctx = {
        "category": category,
        "descendants": list(category.get_descendants()),
        "products_count": len(category.products.all()),
    }
    return TemplateResponse(
        request, "dashboard/category/modal/confirm_delete.html", ctx
    )