Is there a way that instead of pk
i will select the model data by foreign key
in UpdateView? It is no doubt it works on the id
of the data but what if i will select the data object in the model using foreign key
condition? How would i do that?
Here is my UpdateView class:
class SettingsProfileView(UpdateView):
model = UserInfoModel
template_name = 'site/views/settingsprofile.html'
form_class = UserInfoForm
I try to put:
def getquery_set(self):
return self.model.objects.filter(users_id = self.kwargs['pk'])
but still its refering to get the data by primary key
.
The method is .get_queryset(..)
[Django-doc] not , but nevertheless, this will not work, since the filtering is done in the getquery_set
.get_object(..)
method [Django-doc].
You thus can work with:
from django.shortcuts import get_object_or_404
class SettingsProfileView(UpdateView):
model = UserInfoModel
template_name = 'site/views/settingsprofile.html'
form_class = UserInfoForm
def get_object(self, queryset=None):
if queryset is None:
queryset = self.get_queryset()
return get_object_or_404(queryset, user_id=self.kwargs['pk'])