Unable to resolve keyword 'i' in field. The following options are possible: id, join_on, user, user_id

I don't understand what I am doing wrong. I am trying to update a model through a form and I am following a tutorial online, they all point to get the id. I did this, but I keep getting this error:

Unable to resolve keyword 'i' in field. Possible options: id, join_on, user, user_id

the id key is there, but it thinks it is "me" what I'm looking for.

Any idea?

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) 

      

Trace environment:

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

      

Traceback:

        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

      

+3


source to share


1 answer


There is an error in your view on this line,

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

      

Replace it with something like this,



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

      

objects.get

the method takes field_names and values โ€‹โ€‹as keyword arguments and returns an object with a match condition. From your perspective, I assume you want to get the UserProfile of the current loggedin user. To do this, you need to access the user's field identifier (ForeignKey to User) and map it to the current user (request.user).

+4


source







All Articles