0

我有这个:

在模型.py

class Category(models.Model):
    Name = models.CharField(max_length=50)
    Description = models.CharField(max_length=150)

    def __unicode__(self):
        return self.Description

class Subcategory(models.Model):
    Category = models.ForeignKey(Category)
    Name = models.CharField(max_length=50)
    Description = models.CharField(max_length=150)

    def __unicode__(self):
        return self.Description

class Smartgrid(models.Model):
    Category = models.ForeignKey(Category)
    Subcategory = models.ForeignKey(Subcategory)
    Descriptor = models.CharField(max_length=150)

    def __unicode__(self):
        return self.Descriptor

在 forms.py 中:

class SmartgridForm(forms.ModelForm):
    class Meta:
        model = Smartgrid

在views.py

def new_point(request):
    if request.method=='POST':
        form = SmartgridForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/gis')
    else:
        form = SmartgridForm()

    return render_to_response('SmartgridForm.html', {'form':form}, context_instance=RequestContext(request))

问题:

我希望用户在 SmartgridForm.html 中选择类别后,子类别字段仅显示这些子类别,其中类别 = 已选择类别

谢谢 !!!!

JDC。

4

1 回答 1

0

如果您不想使用其他应用程序,可以使用此解决方案:Django/jQuery Cascading Select Boxes?

但对我来说,最好使用 Dajax http://www.dajaxproject.com/forms/

于 2013-09-02T12:55:14.803 回答