On 3/12/2023 7:27 am, David Merrick wrote:
Hi. I can put an item from the database into a select box ie Countries of the World. What I want to know is saying, having chosen New Zealand from the select box, how do I display all the cities of New Zealand in another select box.The database has two tables. First one is Countries. The second table is cities with populations and the foreign key of Countries table.Then having chosen Auckland a city in New Zealand I want to display Country City Population In a template. I have done this in Php, Javascript and Mysql already.
Check https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.html
Also, I recently had a similar task with companies and divisions from querysets and had trouble using the form.__init__() method. Here is an alternative approach. You can tweak what the form displays by calling a form method - here set_division_queryset(). In other words, you can populate selection choices based on code elsewhere. In my case I did it from within the view code like this ...
# forms.py class List_Import(forms.Form): ... division = forms.ModelChoiceField( required=False, empty_label="Must select a Division", queryset=Division.objects.none(), ... def set_division_queryset(self, divqs): self.fields["division"].queryset = divqs
def clean(self): cleaned_data = super().clean() selected_division = cleaned_data.get('division') available_divisions = self.fields['division'].queryset if selected_division not in available_divisions: self.add_error('division', 'Invalid choice. Please select a valid division.') return cleaned_data
# views.py @login_required(redirect_field_name="next") @ensure_csrf_cookie def list_import(request, template="list_import.html", context=None): ... # User model has get_company() method and Company has get_all_divisions() divqs = request.user.get_company().get_all_divisions() form.set_division_queryset(divqs) ... if request.method == 'POST': form = List_Import(request.POST, request.FILES) # because form.is_valid() needs available_divisions and we are # not using form.__init__(), we have to send divqs again form.set_division_queryset(divqs) if form.is_valid(): ... Cheers Mike
Cheers Dave -- Dave Merrick TutorInvercargill http://tutorinvercargill.co.nz Daves Web Designs Website https://tutorinvercargill.co.nz/daveswebdesigns/public_html/ _ _Email merrick...@gmail.com Ph 03 216 2053 Cell 027 3089 169 --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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2B%3DMcKYDfzhvQ8ueCKKccYX3gcQUmfoX9NXipqf%3DDipsXPzG7g%40mail.gmail.com <https://groups.google.com/d/msgid/django-users/CA%2B%3DMcKYDfzhvQ8ueCKKccYX3gcQUmfoX9NXipqf%3DDipsXPzG7g%40mail.gmail.com?utm_medium=email&utm_source=footer>.
-- Signed email is an absolute defence against phishing. This email has been signed with my private key. If you import my public key you can automatically decrypt my signature and be sure it came from me. Your email software can handle signing. -- 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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/edf8f49f-295e-4ccd-a65f-1cad7b35880e%40dewhirst.com.au.
OpenPGP_signature.asc
Description: OpenPGP digital signature