无法解析关键字' i'进入田野。选项包括:id,joined_on,user,user_id

时间:2017-05-25 17:06:31

标签: python django

我不明白我做错了什么。我正在尝试通过表单更新模型,我一直在线学习他们都指向获得“id”的方向。我已经做到了,但我一直收到这个错误:

  

无法解析关键字' i'进入田野。选项包括:id,joined_on,user,user_id

id密钥在那里,但他认为是一个' i'我正在寻找什么。

任何想法?

view.py

fillInDefaults :: Alternative f => Foo f -> f TotalFoo
fillInDefaults = hsequence . (<> defaultFoo)

-- fromJust (unsafely) asserts that there aren't
-- any `Nothing`s in the output of `fillInDefaults`
fillInDefaults' :: PartialFoo -> TotalFoo
fillInDefaults' = fromJust . fillInDefaults

model.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,
                             }
                    )

回溯环境:

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']

1 个答案:

答案 0 :(得分:4)

错误在此行的视图中,

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

用类似的东西替换它,

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

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