Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does flask-admin require a ListField of mongoengine to have a field type?

I am trying to use flask-admin, which seems great and easy, but I have a problem.

I have a field in a collection which is defined as a ListField(), with an implicit type of None for the list field type. The reason I am not defining a type for the field, is because I am keeping a list of lists, and there is no other elegant way (that I found) to accomplish this with mongoengine.

But flask-admin won't let me define such a field, with an error of ListField "movements" must have field specified for model.

Is there a way around this?

like image 298
Oren Solomianik Avatar asked Mar 03 '14 10:03

Oren Solomianik


1 Answers

The reason flask admin needs a field specified is because otherwise the form rendering does not know which type of input to display for it.

For example if it is a choice field, date field, or another list field itself !

You could do something like this:

my_field = db.ListField(field=db.ListField(field=db.StringField()))

The innermost field can be anything, including a EmbeddedDocumentField or IntField, etc.

Also, if you want to continue using ListField without specifying the field type, you can also ask flask-admin to just treat this as a string by overriding the ModelView and it will then just give you a text box containing the string:

[ 'a value', 42, { 'A':'B' } ]

So this retains flexibility but reduces structure and makes the validation bad.

like image 85
AbdealiJK Avatar answered Nov 15 '22 00:11

AbdealiJK