Google App Engine django 模型表单无法识别 BlobProperty

2 投票
2 回答
623 浏览
提问于 2025-04-15 23:55

我有一个这样的模型:

class Image(db.Model):
    auction = db.ReferenceProperty(Auction)
    image = db.BlobProperty()
    thumb = db.BlobProperty()
    caption = db.StringProperty()
    item_to_tag = db.StringProperty()

还有一个这样的表单:

class ImageForm(djangoforms.ModelForm):
    class Meta:
        model = Image

当我调用 ImageForm() 时,只有非 Blob 字段被创建,像这样:

<tr><th><label for="id_auction">Auction:</label></th><td><select name="auction" id="id_auction">
<option value="" selected="selected">---------</option>
<option value="ahRoYXJ0bWFuYXVjdGlvbmVlcmluZ3INCxIHQXVjdGlvbhgKDA">2010-06-19 11:00:00</option>
</select></td></tr>
<tr><th><label for="id_caption">Caption:</label></th><td><input type="text" name="caption" id="id_caption" /></td></tr>
<tr><th><label for="id_item_to_tag">Item to tag:</label></th><td><input type="text" name="item_to_tag" id="id_item_to_tag" /></td></tr>

我希望 Blob 字段也能包含在表单中(作为文件输入)。我哪里做错了呢?

2 个回答

0

你可以使用 widgets 属性来定义你在 blob 属性中使用的字段类型:

class ImageForm(djangoforms.ModelForm):
class Meta:
    model = Image
    widgets = { 
        'image': djangoforms.FileInput(),
        'thumb': djangoforms.FileInput(),
    } 
1

我觉得我的问题主要是因为Django不支持blob类型的数据,所以在生成Django表单的时候,BlobProperty这个属性就被忽略了。

撰写回答