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

1

6

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?

Diverticulitis answered 3/3, 2014 at 10:30 Comment(2)
Any chance you could paste some of your code and show us the version you're using on mongoengine?Jedda
What about a db.ListField(field=db.ListField())Inspection
F
1

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.

Felt answered 8/1, 2016 at 8:21 Comment(1)
Overiding the ModelView ( ... form_overrides = {'movements': StringField} ... ) is a nice workaroundFunereal

© 2022 - 2024 — McMap. All rights reserved.