Django admin site foreign key

63 Views Asked by At

I have created some classes like STATE, DISTRICT, TALUK, and VILLAGE. Admin needs to add details in the admin panel. If the admin needs to add TALUK, he must select provided STATE, DISTRICT.I used a foreign key in the TALUK class for calling states and districts. But in admin after selecting STATE, the DISTRICT dropdown shows all the DISTRICTS. I need to get only the districts of that particular state

This is the code I wrote in models.py

class STATE(models.Model):

    state_name=models.CharField(max_length=25)
    def __str__(self):
        return self.state_name
    
class DISTRICT(models.Model):
    
    district_state=models.ForeignKey(STATE,on_delete=models.CASCADE)
    district_name=models.CharField(max_length=25)
    def __str__(self):
        return self.district_name

class TALUK(models.Model):
    
    taluk_state=models.ForeignKey(STATE,default=1,verbose_name="state",on_delete=models.CASCADE)
    taluk_district=models.ForeignKey(DISTRICT,on_delete=models.CASCADE)
    taluk_name=models.CharField(max_length=25)
    def __str__(self):
        return self.taluk_name

class VILLAGE(models.Model):
    
    taluk_vill=models.ForeignKey(TALUK,on_delete=models.CASCADE)
    vill_name=models.CharField(max_length=25)
    
    def __str__(self):
        return self.vill_name
0

There are 0 best solutions below