get Category object instead of its name in the form
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)
When i go to the form, select drop down men开发者_JAVA技巧u and it only shows "Category object". How can i get the name stored in Category?
Define Category.__unicode__()
.
精彩评论