django FieldError: Cannot resolve keyword 'category_name_contains' into field. Choices are: category, description, id, image, name, order, price

django FieldError: Cannot resolve keyword 'category_name_contains' into field. Choices are: category, description, id, image, name, order, price

我想构建一个送餐应用,但遇到了这个错误。 “FieldError:无法将关键字 'category_name_contains' 解析为字段。选择是:类别 ry、描述、id、图片、名称、订单、价格。

这是query.py:

try:
                model = field.model._meta.concrete_model
            except AttributeError:
                # QuerySet.annotate() may introduce fields that aren't
                # attached to a model.
                model = None
        else:
            # We didn't find the current field, so move position back
            # one step.
            pos -= 1
            if pos == -1 or fail_on_missing:
                available = sorted(
                    [
                        *get_field_names_from_opts(opts),
                        *self.annotation_select,
                        *self._filtered_relations,
                    ]
                )
                raise FieldError(
                    "Cannot resolve keyword '%s' into field. "
                    "Choices are: %s" % (name, ", ".join(available))
                )
            break

这是views.py

from ast import Return
from multiprocessing import context
from django.shortcuts import render
from django.views import View
from .models import MenuItem, Category, OrderModel


class Index(View):
def get(self, request, *args, **kwargs):
    return render(request, 'customer/index.html')


class About(View):
def get(self, request, *args, **kwargs):
    return render(request, 'customer/about.html')


class Order(View):
def get(self, request, *args, **kwargs):
    # get every item from each category
    appetizers = MenuItem.objects.filter(
        category_name_contains='Appetizer')
    main_course = MenuItem.objects.filter(
        category_name_contains='Main Course')
    desserts = MenuItem.objects.filter(category_name_contains='Dessert')
    drinks = MenuItem.objects.filter(category_name_contains='Drink')

    # pass into context
    context = {
        'appetizers': appetizers,
        'main_course': main_course,
        'desserts': desserts,
        'drinks': drinks,
    }
    # render the templates
    return render(request, 'custormer/order.html', context)

def post(self, request, *args, **kwargs):
    order_items = {
        'items': []
    }

    items = request.POST.getlist('items[]')

    for item in items:
        menu_item = MenuItem.objects.get(pk=int(item))
        item_data = {
            'id': menu_item.pk,
            'name': menu_item.name,
            'price': menu_item.price
        }

        order_items['items'].append(item_data)

        price = 0
        item_ids = []

        for item in order_items['items']:
            price += item['price']
            item_ids.append(item['id'])

        order = OrderModel.objects.create(price=price)
        order.items.add(*item_ids)

        context = {
            'items': order_items['items'],
            'price': price

        }

        return render(request, 'customer/order_conformation.html', context)

这是我的 models.py:

from time import strftime
from unicodedata import category, name
from django.db import models


class MenuItem(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
image = models.ImageField(upload_to='menu_image/')
price = models.DecimalField(max_digits=5, decimal_places=2)
category = models.ManyToManyField('Category', related_name='item')

def __str__(self):
    return self.name


class Category(models.Model):
name = models.CharField(max_length=100)

def __str__(self):
    return self.name


class OrderModel(models.Model):
created_on = models.DateTimeField(auto_now_add=True)
price = models.DecimalField(max_digits=7, decimal_places=2)
items = models.ManyToManyField(
    'MenuItem', related_name='order', blank=True)

def __str__(self):
    return f'Order: {self.created_on.strftime("%b %d %I: %M %p")}'

这是回溯:

Traceback (most recent call last):
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\views\generic\base.py", line 84, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\views\generic\base.py", line 119, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\customer\views.py", line 
21, in get
appetizers = MenuItem.objects.filter(
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\db\models\manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\db\models\query.py", line 1071, in filter
return self._filter_or_exclude(False, args, kwargs)
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\db\models\query.py", line 1089, in _filter_or_exclude
clone._filter_or_exclude_inplace(negate, args, kwargs)
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\db\models\query.py", line 1096, in _filter_or_exclude_inplace
self._query.add_q(Q(*args, **kwargs))
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\db\models\sql\query.py", line 1502, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\db\models\sql\query.py", line 1532, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\db\models\sql\query.py", line 1377, in build_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\db\models\sql\query.py", line 1187, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "C:\Users\Shreya\OneDrive\Desktop\li'lbites\littlebites\.venv\lib\site- 
packages\django\db\models\sql\query.py", line 1677, in names_to_path
raise FieldError(

Exception Type: FieldError at /order/
Exception Value: Cannot resolve keyword 'category_name_contains' into field. Choices 
are: category, description, id, image, name, order, price

所以这是我的问题,我尝试了很多解决方案仍然没有解决问题。正如我所看到的,我的核心代码没有问题,但存在一些内部问题,但我仍然无法 运行 我的程序不解决它们。

你快到了。 你需要加倍 _ 符号

category_name_containscategory_name__contains

错误是完全有道理的,这是因为你与你的字段 categoryManyToMany 关系,而你通过 filter(category_name_contains) 进行过滤,这根本不正确。

字段查找需要 __ 作为前缀而不是 _。所以一定是__contains.

您应该对每个条件使用 filter(category__name__contains='Appetizer')

如果您只想通过名称进行过滤,那么您可以简单地使用以下方式进行过滤:

Views.py


class Order(View):
def get(self, request, *args, **kwargs):
    # get every item from each category
    appetizers = MenuItem.objects.filter(
        category__name__contains='Appetizer')
    main_course = MenuItem.objects.filter(
        category__name__contains='Main Course')
    desserts = MenuItem.objects.filter(category__name__contains='Dessert')
    drinks = MenuItem.objects.filter(category__name__contains='Drink')

    # pass into context
    context = {
        'appetizers': appetizers,
        'main_course': main_course,
        'desserts': desserts,
        'drinks': drinks,
    }
    # render the templates
    return render(request, 'custormer/order.html', context)

Note: ManyToManyField required its name to be defined as table_name with s to be the suffix, that means plural of table name. It will be better if you name it as categories instead of category in your MenuItem model.

Note: Models doesn't require model to be the suffix, so it will be better if you name it as Order instead of OrderModel.