在表单中获取Category对象而不是其名称

时间:2011-08-15 03:51:13

标签: django forms

model.py

class Category(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)

def __unicode__(self):
  return self.name

 class Item(models.Model):
  listing = models.CharField(max_length=1, choices=LISTING_TYPES, default='T')
  name = models.CharField(max_length=255)
  category = models.ForeignKey(Category)
  department = models.CharField(max_length=255)
  description = models.TextField()
  posted_on = models.DateTimeField(auto_now_add=True)

forms.py

 class ItemForm(forms.Form):
   listing = forms.ChoiceField(choices=LISTING_TYPES, initial='T')
   name = forms.CharField(max_length=255)
   category = forms.ModelChoiceField(Category.objects.all())
   department = forms.CharField(max_length=255)
   description = forms.CharField(widget=forms.Textarea)

当我转到表单时,选择下拉菜单,它只显示“类别对象”。如何将名称存储在Category?

1 个答案:

答案 0 :(得分:1)