2017-05-25 58 views
3

我不明白我做錯了什麼。我試圖通過表單更新模型,並且我一直在網上學習,他們都指向了獲得'id'的方向。我已經做了,但我不斷收到此錯誤:無法將關鍵字'i'解析爲字段。選擇是:id,joined_on,user,user_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, 
          } 
        ) 

model.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 

回答

4

的錯誤是在這一行你的觀點,

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

像這樣的東西代替它,

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

objects.get方法需要FIELD_NAMES和值關鍵字參數並返回匹配條件的對象。 在你看來,我想你想獲得當前登錄用戶的UserProfile的ID。爲此,您需要訪問用戶字段的ID(ForeignKey to User)並將其與當前用戶(request.user)進行匹配。

+0

爲了我的理解,我需要檢索與此特定用戶關聯的數據,以便通過表單將這些數據更新到數據庫中。我試過你的解決方案,並且我得到'UserProfileModel匹配查詢不存在。'在數據庫中它確實存在。 – gcc

+0

您確定用戶的UserProfile模型確實存在於數據庫中嗎? – zaidfazil

+0

我的不好,試試這個'user__username = request.user.username',我忘了把雙下劃線**「__」**有 – zaidfazil

相關問題