从模型扩展的Django表单不起作用

时间:2013-04-17 17:36:52

标签: django django-models django-forms

我想创建一个继承自我的模型的表单,但它不起作用,我找不到原因。

它不会抛出任何异常,但只是不显示指定的字段。

models.py

class Picture(models.Model):
    picture = models.ImageField(upload_to='/')
    name = models.CharField(max_length=30)
    owner = models.ForeignKey(Artist)
    date = models.DateField()
    description = models.CharField(max_length=1000)
    width = models.IntegerField(max_length=9)
    height = models.IntegerField(max_length=9)
    sold = models.BooleanField(default=False)
    added = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField(default=False)
    genres = models.ManyToManyField(Genre)
    def __unicode__(self):
        return u'%s from %s by %s' % (self.name, self.date, self.owner)

forms.py

class PictureForm(forms.Form):
    class Meta:
        model = Picture
        fields = ('picture', 'name', 'date', 'description', 'width', 'height')

我错过了什么,或者是否存在一般错误?

1 个答案:

答案 0 :(得分:2)

您使用的是forms.Form而不是forms.ModelForm

class PictureForm(forms.ModelForm):
    class Meta:
        model = Picture
        fields = ('picture', 'name', 'date', 'description', 'width', 'height')