Hello Balaji Sir,

AFAIK, Django does not filter the foreign key fields by default, Cascading
Drop Down is not available by default in django.
If the filtering was to be done on the initial value only, you could have
overwritten the __init__() in the form to filter the fk values.
But that does not seem to be the case, as the filtering needs to be done on
the clientside.

One solution that I know will work for sure is -
Write custom javascript code to add event listeners on the select dropdown.
Whenever an entry is selected,
AJAX request would be sent to server to fetch only the related/required
fields for the current selection and
show these fetched values in the dropdown. Use of jquery would be optimal
for this.
Hence, you would write separate endpoints to fetch the filtered list also.

Another solution would be output the values and their relations to a
javascript variables in the template.
Then show the select dropdown based on these values. This one can be
cumbersome to understand as well as implement.
I am not sure if it will work or not :P

Go for the first solution :)


Regards,
Chetan Ganji
+91-900-483-4183
ganji.che...@gmail.com
http://ryucoder.in


On Wed, May 22, 2019 at 11:22 AM Balaji Shetty <balajishe...@gmail.com>
wrote:

> Hi
>
> I have registered model in admin.py
> Added Continent  ( Cont1, Cont2, Cont3 )
>
> Added Country under Continent
>
> Cont1 - Count11
> Cont1- Count12
>
> Cont2 - Count21
> Cont2-Count22
>
> Con3-Count31
> Con3-Count31
>
> When I add Location
>
> When i select Cont1, I should get only Count11 , Count12
>
> When i select Cont2, I should get only Count21 , Count22
>
> When i select Cont3, I should get only Count31 , Count32
>
> But I get all Countries under all Continent. Cascading dependency are not
> shown
>
> I tried different option but lot of issues are there .
>
>
>
> Example
>
>
>
> On Wed, May 22, 2019 at 10:56 AM Nitin Kumar <nitinkumar...@gmail.com>
> wrote:
>
>> Everything seems alright. It seems you haven't created any continents
>> yet. The table is empty.
>>
>> On Wed, 22 May, 2019, 10:52 AM Balaji Shetty <balajishe...@gmail.com
>> wrote:
>>
>>> Hi
>>>
>>> I am learning Django from last months and want to implement Cascading
>>> Drop Down for Continent and  Country  in Admin GUI.
>>>
>>> I dropped same query two times but i could not get exact correct
>>> solution.
>>>
>>> I tried lot of option like
>>> django-smart-select and many more..
>>>
>>> But i could not get updates in drop down.
>>>
>>>  Can Any Django Expert solve this issue? Your help would be highly
>>> appreciated.
>>>
>>> This is sample Model for  demonstration of the schema
>>> models.py
>>> ----------------------------------------------------------
>>>
>>>
>>> class Continent(models.Model):
>>>     name = models.CharField(max_length=255)
>>>
>>>     def __str__(self):
>>>         return self.name
>>>
>>>
>>> class Country(models.Model):
>>>     continent = models.ForeignKey(Continent,on_delete=models.CASCADE)
>>>     name = models.CharField(max_length=255)
>>>
>>>     def __str__(self):
>>>         return self.name
>>>
>>> class Location(models.Model):
>>>     continent = models.ForeignKey(Continent,on_delete=models.CASCADE)
>>>     country = models.ForeignKey(Country,on_delete=models.CASCADE)
>>>     city = models.CharField(max_length=50)
>>>     street = models.CharField(max_length=100)
>>>
>>>     def __str__(self):
>>>         return self.city
>>>
>>>
>>>
>>>
>>> --
>>>
>>>
>>> *Mr. Shetty Balaji S.Asst. ProfessorDepartment of Information
>>> Technology,*
>>> *SGGS Institute of Engineering & Technology, Vishnupuri, Nanded.MH.India*
>>> *Official: bsshe...@sggs.ac.in <bsshe...@sggs.ac.in> *
>>> *  Mobile: +91-9270696267*
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/CAECSbOtoje0sdMsoeQvCO-mq-h-AQVOKtnYA3wFnLAE-tPhXGg%40mail.gmail.com
>>> <https://groups.google.com/d/msgid/django-users/CAECSbOtoje0sdMsoeQvCO-mq-h-AQVOKtnYA3wFnLAE-tPhXGg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@googlegroups.com.
>> Visit this group at https://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAKzNicGqspLgrFnYf%3D%2B22s8SJUc2uVTt5223UJ7Ba1d6ZbtqbA%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAKzNicGqspLgrFnYf%3D%2B22s8SJUc2uVTt5223UJ7Ba1d6ZbtqbA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
> --
>
>
> *Mr. Shetty Balaji S.Asst. ProfessorDepartment of Information Technology,*
> *SGGS Institute of Engineering & Technology, Vishnupuri, Nanded.MH.India*
> *Official: bsshe...@sggs.ac.in <bsshe...@sggs.ac.in> *
> *  Mobile: +91-9270696267*
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAECSbOsdkJZ%3DPT7vxyt1K5c2%3D-%2B9_squ5SES0zK_vD5Dyo97Bg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAECSbOsdkJZ%3DPT7vxyt1K5c2%3D-%2B9_squ5SES0zK_vD5Dyo97Bg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAMKMUjsjB%2BSswY_e3voB4OpPwZ%2BRpDukdGedc6Lin5w6hm5RoQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to