开发者

How to create Form from a Model which has a ListProperty

I am currently using Django forms with the Google App Engine and I have a model which is as follows:

class Menu(db.Model):
    name = db.StringProperty(required=True)
    is_special = db.BooleanProperty()
    menu_items =  db.ListProperty(MenuItem)

I have a MenuForm which is the following:

class MenuForm(djangoforms.ModelForm):
    cla开发者_运维问答ss Meta:
        model = Menu
        exclude = ['added_by','menu_items']

When I run this I get the following error:

Exception Type:     ValueError
Exception Value:    Item type MenuItem is not acceptable

I want to crate the form and have it omit the menu_items property as for one I don't think there is an in built control for the multiple choice, like a group of check boxes. Either way I cannot understand with this property in the exclude items why it is throwing this error.

TIA

Andrew


Your problem comes well before the "create a form" task begins: ListProperty does not allow a list of model entities (although I can't find this clearly documented in the app engine docs, I'm still looking in the docs for a good, clear, unambiguous statement about that). Try changing it into (say) a list of strings, and you'll see everything works (I believe a dropdown is what you get if you don't exclude such a property).

Edit: found the spot in the docs where the issue is mentioned, although it's quaintly phrased -- quoting with added emphasis:

The list can contain values of any of the value types supported by the datastore.

...point is, you can have in the list objects of any of the value types... not reference ones, i.e., entities that are instances of some model.

Could you use a list of key strings, instead...?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜