这是我的forms.py
from django import forms
from django.utils.translation import ugettext_lazy as _
from category.models import *
class ItemForm(forms.Form):
category = forms.ModelChoiceField(queryset=Category.objects.filter(is_active=True).order_by('category_text'),label=_("Category"))
subcategory = forms.ModelChoiceField(queryset=Subcategory.objects.all().order_by('subcategory_text'),label=_("Subcategory"))
item = forms.CharField(widget=forms.TextInput(attrs=dict(required=True, max_length=30, render_value=False)), label=_("Item")
这是models.py
from django.db import models
class Category(models.Model):
category_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
is_active = models.BooleanField()
def __str__(self):
return self.category_text
class Subcategory(models.Model):
category = models.ForeignKey(Category)
subcategory_text = models.CharField(max_length=200)
def __str__(self):
return self.subcategory_text
我想首先从category
选择dropdown
,然后根据category
subcategory
下category
我要加载的Subcategory
ajax
}`dropdown。
我不知道如何从request
发出django form
dropdown
并将响应数据加载回form
中的/
。
提前感谢您的帮助。