gpt4 book ai didi

python - 无法将关键字 'i' 解析为字段。选择是 : id, joined_on, user, user_id

转载 作者:太空宇宙 更新时间:2023-11-03 14:53:53 26 4
gpt4 key购买 nike

我不明白我做错了什么。我正在尝试通过表单更新模型,并且我一直在关注在线教程,它们都指向获取“id”的方向。我已经完成了,但我一直收到此错误:

Cannot resolve keyword 'i' into field. Choices are: id, joined_on, user, user_id

id 键在那里,但他认为是我正在寻找的“i”。

有什么想法吗?

view.py

def testRegistration(request):
id = UserProfileModel.objects.get('id')
user_status_form = UserDetailsForm(request.POST or None, instance=id)
if request.method == 'POST':
if user_status_form.is_valid():
user_status = user_status_form.save(commit=False)
user_status.user = get_user(request)
user_status.save()
user_status_form = UserDetailsForm()
else:
user_status_form = UserDetailsForm()

return HttpResponseRedirect('testRegistration')

return render(
request, 'registrationTest.html',
{'user_status_form' : user_status_form,
}
)

模型.py

class UserProfileModel(models.Model):
user = models.OneToOneField(User, unique=True)
joined_on = models.DateTimeField(auto_now=True, null=True)

回溯环境:

        Request Method: GET
Request URL: http://127.0.0.1:8000/testRegistration

Django Version: 1.10.5
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app']
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']

回溯:

        File "/Applications/anaconda/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
39. response = get_response(request)

File "/Applications/anaconda/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)

File "/Applications/anaconda/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/xxx/xxx/xxx/app/views.py" in testRegistration
88. id = UserProfileModel.objects.get('id')

File "/Applications/anaconda/lib/python3.5/site-packages/django/db/models/manager.py" in manager_method
85. return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/Applications/anaconda/lib/python3.5/site-packages/django/db/models/query.py" in get
376. clone = self.filter(*args, **kwargs)

File "/Applications/anaconda/lib/python3.5/site-packages/django/db/models/query.py" in filter
796. return self._filter_or_exclude(False, *args, **kwargs)

File "/Applications/anaconda/lib/python3.5/site-packages/django/db/models/query.py" in _filter_or_exclude
814. clone.query.add_q(Q(*args, **kwargs))

File "/Applications/anaconda/lib/python3.5/site-packages/django/db/models/sql/query.py" in add_q
1227. clause, _ = self._add_q(q_object, self.used_aliases)

File "/Applications/anaconda/lib/python3.5/site-packages/django/db/models/sql/query.py" in _add_q
1253. allow_joins=allow_joins, split_subq=split_subq,

File "/Applications/anaconda/lib/python3.5/site-packages/django/db/models/sql/query.py" in build_filter
1133. lookups, parts, reffed_expression = self.solve_lookup_type(arg)

File "/Applications/anaconda/lib/python3.5/site-packages/django/db/models/sql/query.py" in solve_lookup_type
1019. _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())

File "/Applications/anaconda/lib/python3.5/site-packages/django/db/models/sql/query.py" in names_to_path
1327. "Choices are: %s" % (name, ", ".join(available)))

Exception Type: FieldError at /testRegistration
Exception Value: Cannot resolve keyword 'i' into field. Choices are: id, joined_on, user, user_id

最佳答案

错误在你看来是在这一行,

id = UserProfileModel.objects.get('id')

用这样的东西代替它,

id = UserProfileModel.objects.get(user__username=request.user.username)

objects.get 方法将 field_names 和 values 作为关键字参数并返回具有匹配条件的对象。从您的角度来看,我想您想要获取当前登录用户的 UserProfile 的 ID。为此,您需要访问用户字段的 ID(用户的 ForeignKey)并将其与当前用户(request.user)匹配。

关于python - 无法将关键字 'i' 解析为字段。选择是 : id, joined_on, user, user_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44186073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com