'I get the error "Cannot resolve keyword 'category' into field. Choices are: id" in MPTT
I want to create a subcategory with MPTT but I get the error "Cannot resolve keyword 'category' into field. Choices are: id". I can't find the reason. I would be happy if you half. I'm trying to edit dressing on the admin page. I'm trying to do this from a source I found on the internet. source:https://django-mptt.readthedocs.io/en/latest/models.html
models.py
from django.db import models
from mptt.models import MPTTModel, TreeForeignKey
# Create your models here.
class Category(MPTTModel):
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True)
keywords = models.CharField(max_length=100)
parent = TreeForeignKey('self', on_delete=models.CASCADE, blank=True, null=True,
related_name='children')
class MPTTMeta:
order_insertion_by = ['name']
def __str__(self):
return self.name
class Product(models.Model):
pass
admin.py
from django.contrib import admin
from mptt.admin import DraggableMPTTAdmin
from .models import Category, Product
# Register your models here.
class CategoryAdmin(DraggableMPTTAdmin):
prepopulated_fields = {'slug': ('name',)}
mptt_indent_field = "name"
list_display = ('tree_actions', 'indented_title',
'related_products_count', 'related_products_cumulative_count')
list_display_links = ('indented_title',)
def get_queryset(self, request):
qs = super().get_queryset(request)
qs = Category.objects.add_related_count(
qs,
Product,
'category',
'products_cumulative_count',
cumulative=True)
qs = Category.objects.add_related_count(qs,
Product,
'categories',
'products_count',
cumulative=False)
return qs
def related_products_count(self, instance):
return instance.products_count
related_products_count.short_description = 'Kataqoriada olan məhsulların sayı'
def related_products_cumulative_count(self, instance):
return instance.products_cumulative_count
related_products_cumulative_count.short_description = 'Alt kataqoriada olan məhsulların sayı'
admin.site.register(Product)
admin.site.register(Category, CategoryAdmin)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

