Trying to filter of data based upon child's child model set Django

Trying to create a function based where the executive get's options based upon the pincodes. Tried this method but it's not working out can anyone please help? Thanks in advance... (Don't judge the code because this a early stage development and I'm a beginner developer so sorry if the code isn't proper to read)

Models.py

class User(AbstractUser):
ADDCHKS_ID=models.CharField(max_length=16)
is_active=models.BooleanField(default=False)
is_excutive=models.BooleanField(default=False)

class ExcutiveRegistration(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
'''Personal Details'''
First_name = models.CharField(max_length=100)
Last_name = models.CharField(max_length=100)
DOB = models.DateField(max_length=100)
Fathers_name = models.CharField(max_length=100)
Contact_number = models.IntegerField(max_length=10)
Alt_Contact_number = models.IntegerField(max_length=10)
# Profile_picture=models.ImageField(upload_to='images/', null=True, verbose_name="")
'''Current Addresss'''
Pin_code_c = models.IntegerField(max_length=6)
State_c = models.CharField(max_length=50)
District_c = models.CharField(max_length=50)
Taluk_c = models.CharField(max_length=50)
Location_c = models.CharField(max_length=100)
House_no_c = models.IntegerField(max_length=4)
Street_c = models.CharField(max_length=100)
'''Permanent Address'''
Pin_code_p = models.IntegerField(max_length=6, null=True)
State_p = models.CharField(max_length=50, null=True)
District_p = models.CharField(max_length=50, null=True)
Taluk_p = models.CharField(max_length=50, null=True)
Location_p = models.CharField(max_length=100, null=True)
House_no_p = models.IntegerField(max_length=4, null=True)
Street_p = models.CharField(max_length=100, null=True)
'''Reference'''
Family_reference_name = models.CharField(max_length=100, null=True)
Family_reference_conatact_number = models.IntegerField(max_length=10, null=True)
Neighbour_reference_name = models.CharField(max_length=100, null=True)
Neighbour_reference_contact = models.IntegerField(max_length=10, null=True)
'''Document Proof'''
ID_Proof_Document_Type=models.CharField(max_length=50,
                                        choices=ID_CHOICES,
                                        default="Voter",
                                        null=True)

class ExcutiveRegistrationPincode(models.Model):
ExcutiveRegistration=models.ForeignKey(ExcutiveRegistration, on_delete=models.CASCADE)
Covering_Pincode=models.IntegerField(max_length=6)

class Customer(models.Model):
# user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
Verification=models.CharField(max_length=50,
                              choices=Verification_Method,
                              default=None,
                              null=True,
                              blank=True)
Client_ID=models.CharField(max_length=20, null=True,blank=True)
Client_name=models.CharField(max_length=50,blank=True)
Applicant_Name=models.CharField(max_length=50,blank=True)
DOB=models.DateField()
Father_name=models.CharField(max_length=50,  null=True,blank=True)
Pincode=models.IntegerField(max_length=6, null=True,blank=True)
Present_address=models.CharField(max_length=300, null=True,blank=True)
Permanent_address=models.CharField(max_length=300, null=True,blank=True)
Contact_number=models.CharField(max_length=11, null=True,blank=True)
Alt_Contact_number=models.CharField(max_length=11, null=True,blank=True)
Selfie_with_residence=models.ImageField(upload_to="images/Client_verification", null=True,blank=True)
Surrounding_left=models.ImageField(upload_to="images/Client_verification", null=True,blank=True)
Surrounding_right=models.ImageField(upload_to="images/Client_verification", null=True,)
Landmark=models.ImageField(upload_to="images/Client_verification", null=True,blank=True)
NID_proof=models.CharField(max_length=50,
                            choices=ID_CHOICES,
                            default=None,
                            null=True,
                            blank=True)
NID_proof_Image=models.ImageField(upload_to="images/Client_verification", null=True,blank=True)
NID_proof_Image_2=models.ImageField(null=True, blank=True, upload_to="images/Client_verification")
Candidate_Signature=models.ImageField(upload_to="images/Client_verification", null=True,blank=True)
Ref_no=models.CharField(max_length=16, null=True,blank=True)
BGV_Intiation_date=models.DateField(null=True)
Excutive_worked=models.ForeignKey(ExcutiveRegistration,models.SET_NULL,null=True,related_name='Excutive_worked',blank=True)
Excutive_Reworked=models.ForeignKey(ExcutiveRegistration,models.SET_NULL,null=True,related_name='Excutive_Reworked',blank=True)
Date_of_work_compeleted=models.DateField(null=True,blank=True)
Days_of_work_postponed=models.DateField(null=True,blank=True)
Status=models.CharField(max_length=50,
                        choices=Status__,
                        default='null',
                        null=True,
                        blank=True)
Assign=models.BooleanField(default=False,blank=True)
def __str__(self):
    return self.Client_ID + self.Client_name

views.py

class excutive_home_page(View):
@login_required(login_url='login')
def get(self, *args, **kwargs):
    current_user=request.user
    str_current_user=str(current_user.id)
    if current_user:
        get_excutive=ExcutiveRegistration.objects.filter(pk=str_current_user)
        str_get_excutive=str(get_excutive)
        get_excutive_pincode=ExcutiveRegistrationPincode.objects.filter(pk=str_get_excutive)
        str_get_excutive_pincode=str(get_excutive_pincode)
        customers = Customer.objects.filter(Pincodes=str_get_excutive_pincode)
        context = {
        "customers":customers,
        }
        return render(self.request, 'excutive\excutive_home_page.html', context)
   
    return HttpResponse(self.request, "try someother way")

excutive_home_page.html

hi {{request.user}}
    
  {% for customer in customers  %}
  {{customer}} 

You need to obtain the Covering_Pincode from the ExcutiveRegistrationPincodes, not use str(…). But by making use of IntegerFields over just ForeignKeys, 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=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=request.user
            ).only('Covering_Pincode')
        ]
        return Customer.objects.filter(Pincodes__in=pincodes)
Back to Top