我想为汽车细节创建动态下拉菜单
所以我创造了四个模型 我的models.py
class CompanyMake(models.Model):
company_name = models.CharField(max_length = 100)
def __unicode__(self):
return self.company_name
class MakeModel(models.Model):
company = models.ForeignKey(CompanyMake)
model_name = models.CharField(max_length = 100)
def __unicode__(self):
return self.model_name
class ModelParts(models.Model):
company = models.ForeignKey(CompanyMake)
model = models.ForeignKey(MakeModel)
part_name = models.CharField(max_length = 100)
def __unicode__(self):
return self.part_name
class ModelYear(models.Model):
company = models.ForeignKey(CompanyMake)
model = models.ForeignKey(MakeModel)
year_value = models.IntegerField()
class UserProfile(models.Model):
user = models.OneToOneField(User,primary_key=True)
#image = models.ImageField(upload_to=get_photo_storage_path, null = True, blank=True)
part = models.ForeignKey(ModelParts,null = True, blank=True)
phone_number = models.CharField( max_length = 10,verbose_name="Mobile Number 10 digits",null = True, blank=True, default=None)
现在我想创建汽车,型号,零件和年份的下拉菜单 如果特定汽车选择了与该汽车相关的所有型号应该进入下一个型号下拉列表,如果选择了特定的下拉列表,则与该型号相关的所有部件应该进入下一部分下拉菜单中。 / p>
我的views.py
def home(request):
cm = CompanyMake.objects.all()
for a in cm:
print a
mm = a.makemodel_set.all()
for b in mm:
print b
mp = b.modelparts_set.all()
for c in mp:
print c
my = ModelYear.objects.all()
print my
return render(request,'choice.html',{'cm':cm,'mm':mm, 'mp':mp, 'my':my})
我的choice.html文件是
<select name="car">
{% for car in cm %}
<option value="{{car.key}}" selected="selected">{{car.company_name}}</option>
{% endfor %}
</select>
<br/>
请指导我正确的方向。我只得到汽车模型下拉菜单。在上面的方式我可以得到所有的下拉菜单,但它不会相互关联。 我想按相关顺序下拉菜单。对于汽车相关的所有型号都应该在那里,对于模型,所有相关部件都应该在那里。