Reputation: 75
I do not understand what I am doing wrong. I am trying to update a model through the form and I have been following tutorial online they all point in the direction of getting the 'id'. I have done it but I keep getting this error:
Cannot resolve keyword 'i' into field. Choices are: id, joined_on, user, user_id
the id key is there, but he thinks is an 'i' what I am 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)
Traceback 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
Upvotes: 5
Views: 7388
Reputation: 9235
The error is in your view in this line,
id = UserProfileModel.objects.get('id')
Replace it with something like this,
id = UserProfileModel.objects.get(user__username=request.user.username)
objects.get
method takes field_names and values as keyword arguments and returns an object with matching condition.
From your view, I suppose you want to get the ID of UserProfile of the currently loggedin user. For that you need to access the id of user field(ForeignKey to User) and match it with current user(request.user).
Upvotes: 6