you must to use Ajax to populate the second dropdown
Rafael E. Ferrero
2016-01-29 14:08 GMT-03:00 Andrea Pinna <andreapinna@gmail.com>:
Dear all,--
I'm very unexperienced with Django but at the same time I'm trying hard to learn as much as possible.
In my website I have a form with two dropdown menus, and I'd like to make the second (items) dependent by the first (categories).
As an example, in the first dropdown the possible choices are FRUITS and VEGETABLES.
When the user selects FRUITS, I'd like the second dropdown to show only ORANGE, APPLE, PEAR, etc.
Else, when the user selects VEGETABLES, I'd like the second dropdown to show only LETTUCE, CABBAGE, CARROT, etc
My current code follows (I kept it simple): the form shows two independent dropdown menus.
Unfortunately I've not been able to adjust it as requested by django-smart-selects, e.g. by correctly modifying the Item model and the forms.py file.
Could anyone help me, please?
*********************************************
The choices are stored in a MySQL database in two tables:
Types
ID Name
1 FRUIT
2 VEGETABLE
Items
ID Type_ID Name
1 1 Orange
2 1 Apple
3 1 Pear
4 2 Lettuce
5 2 Cabbage
6 2 Carrot
Each item is therefore associated to a unique type (fruit or vegetable).
*********************************************
My models.py consists of:
from django.db import models
from smart_selects.db_fields import ChainedForeignKey
class Type(models.Model):
name = models.CharField(max_length=20)
class Item(models.Model):
name = models.CharField(max_length=20)
type = models.ForeignKey(Type, on_delete=models.CASCADE)
*********************************************
My forms.py is:
from django import forms
from .models import Type, Item
class SelectForm(forms.Form):
q = Type.objects.all()
qf = forms.ModelChoiceField(queryset = q, empty_label = None)
s = Item.objects.all()
sf = forms.ModelChoiceField(queryset = s, empty_label = None)
*********************************************
My views.py is:
from django.shortcuts import render
from .forms import SelectForm
def index(request):
form = SelectForm()
context = {'f': form}
return render(request, 'selects/test.html', context)
*********************************************
Finally, my template is:
<html>
<head>
<title>Select data</title>
</head>
<body>
<form action="/show_results/" method="get">
{{f.as_p}}
<input type="submit" value="Submit">
</form>
</body>
</html>
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+unsubscribe@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/e8641d09-8c04-4b70-bd64-f13cfab50ae7%40googlegroups.com.
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+unsubscribe@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/CAJJc_8UbCeyLWLqHT4aUKpPFcV2abUSHBR14EgFCkqkvT5U-Mg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment