1👍
✅
You need to obtain the Covering_Pincode
from the ExcutiveRegistrationPincode
s, not use str(…)
. But by making use of IntegerField
s over just ForeignKey
s, you make it harder to query, and you can not use the Django ORM effectively.
You can also simplify work by making use of a TemplateView
[Django-doc]:
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView
class ExcutiveHomeView(LoginRequiredMixin, TemplateView):
template_name = 'excutive/excutive_home_page.html'
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
pincodes = [
e.Covering_Pincode
for e in ExcutiveRegistrationPincode.objects.filter(
ExcutiveRegistration__user=self.request.user
).only('Covering_Pincode')
]
context['customers'] = Customer.objects.filter(Pincodes__in=pincodes)
return context
or even better with a simple ListView
[Django-doc]:
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView
class ExcutiveHomeView(LoginRequiredMixin, ListView):
context_object_name = 'customers'
template_name = 'excutive/excutive_home_page.html'
def get_queryset(self, *args, **kwargs):
pincodes = [
e.Covering_Pincode
for e in ExcutiveRegistrationPincode.objects.filter(
ExcutiveRegistration__user=self.request.user
).only('Covering_Pincode')
]
return Customer.objects.filter(Pincodes__in=pincodes)
Source:stackexchange.com