【问题标题】:Django Cannot resolve keyword 'AuctionListing' into field. Choices are: auctionitem, auctionitem_id, currentbid, id, usernameb, usernameb_idDjango 无法将关键字“AuctionListing”解析为字段。选项有:auctionitem、auctionitem_id、currentbid、id、usernameb、usernameb_id
【发布时间】:2020-10-13 05:43:40
【问题描述】:

这是我的模型:

from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):
    pass

class AuctionListing(models.Model):
    username = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user")
    title = models.CharField(max_length=64)
    description = models.CharField(max_length=1000)
    startingbid = models.DecimalField(max_digits=6, decimal_places=2)
    category = models.CharField(max_length=64, blank=True)
    imgurl = models.URLField(max_length=300, blank=True)
    status = models.BooleanField(default=True)

class Bid(models.Model):
    usernameb = models.ForeignKey(User, on_delete=models.CASCADE, related_name="userb")
    auctionitem = models.ForeignKey(AuctionListing, on_delete=models.CASCADE, related_name="item_bided", default=1)
    currentbid = models.DecimalField(max_digits=6, decimal_places=2, default=0)

这是我的看法:

def login_view(request):
if request.method == "POST":

    # Attempt to sign user in
    username = request.POST["username"]
    password = request.POST["password"]
    user = authenticate(request, username=username, password=password)

    # Check if authentication successful
    if user is not None:
        login(request, user)
        alfa_list = Bid.objects.filter(AuctionListing__status=True)
        return render(request, "auctions/index.html", {
            "alfa_list":alfa_list
        })
        ...

当我运行 login_view 时,我收到错误“无法将关键字 'AuctionListing' 解析到字段中。选择有:auctionitem、auctionitem_id、currentbid、id、usernameb、usernameb_id”

任何想法为什么?是模特吗?是视图中生成 alfa_list 的代码吗?我的想法不多了...

这是回溯

Environment:

Request Method: POST
Request URL: http://127.0.0.1:8000/login

Django Version: 3.1.1
Python Version: 3.8.5
Installed Applications:
['auctions',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback (most recent call last):
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\LINA\Documents\CS50\commerce\auctions\views.py", line 48, in login_view
    alfa_list = Bid.objects.filter(AuctionListing__status=True)
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\db\models\query.py", line 942, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\db\models\query.py", line 962, in _filter_or_exclude
    clone._filter_or_exclude_inplace(negate, *args, **kwargs)
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\db\models\query.py", line 969, in _filter_or_exclude_inplace
    self._query.add_q(Q(*args, **kwargs))
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\db\models\sql\query.py", line 1358, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
    child_clause, needed_inner = self.build_filter(
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\db\models\sql\query.py", line 1258, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\db\models\sql\query.py", line 1084, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "C:\Users\LINA\AppData\Roaming\Python\Python38\site-packages\django\db\models\sql\query.py", line 1481, in names_to_path
    raise FieldError("Cannot resolve keyword '%s' into field. "

Exception Type: FieldError at /login
Exception Value: Cannot resolve keyword 'AuctionListing' into field. Choices are: auctionitem, auctionitem_id, currentbid, id, usernameb, usernameb_id

谢谢!!

【问题讨论】:

    标签: django join django-models django-orm


    【解决方案1】:

    你的查询应该是这样的。

    alfa_list = Bid.objects.filter(<b>auctionitem</b>__status=True)

    【讨论】:

    • 感谢您的回答!实际上,这是例外值中所述的一种选择。但是,我试图理解的是这段代码产生错误的原因。我在其他stackoverflow聊天中看到人们设法使用与此类似的代码:alfa_list = Bid.objects.filter(AuctionListing__status=True),但在这种情况下它不起作用:(所以,我仍然想知道为什么.
    • 因为这里您指的是AuctionListing 模型,字段为auctionitem
    猜你喜欢
    • 2012-09-13
    • 2021-11-04
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    相关资源
    最近更新 更多