Repository: flask-admin/flask-admin

Issue:
Issue with Inline Form Choices with Enum Field
In a database, it's normal not to know in what order `enum` fields are shown because they are sorted by their index value. However, using `form_choices`, it is possible to constrain the order of presentation of an `enum` field within the `CREATE` form.

Sadly, this is not working for inline editing. On [this gist](https://gist.github.com/boolbag/ec2bdd25ac5de0c12b63), if you try to edit the `favorite_day` field **inline**, you get a "random" order of days. In contrast, on the `CREATE` field, the options set in `form_choices` are respected.



Discussion:

Resolving patch:
diff --git a/flask_admin/model/widgets.py b/flask_admin/model/widgets.py
--- a/flask_admin/model/widgets.py
+++ b/flask_admin/model/widgets.py
@@ -2,7 +2,7 @@
 from jinja2 import escape
 from wtforms.widgets import HTMLString, html_params
 
-from flask_admin._compat import as_unicode
+from flask_admin._compat import as_unicode, text_type
 from flask_admin.babel import gettext
 from flask_admin.helpers import get_url
 from flask_admin.form import RenderTemplateWidget
@@ -113,7 +113,14 @@ def get_kwargs(self, subfield, kwargs):
             kwargs['data-role'] = 'x-editable-boolean'
         elif subfield.type == 'Select2Field':
             kwargs['data-type'] = 'select'
-            kwargs['data-source'] = dict(subfield.choices)
+            choices = [{'value': x, 'text': y} for x, y in subfield.choices]
+
+            # prepend a blank field to choices if allow_blank = True
+            if getattr(subfield, 'allow_blank', False):
+                choices.insert(0, {'value': '__None', 'text': ''})
+
+            # json.dumps fixes issue with unicode strings not loading correctly
+            kwargs['data-source'] = json.dumps(choices)
         elif subfield.type == 'DateField':
             kwargs['data-type'] = 'combodate'
             kwargs['data-format'] = 'YYYY-MM-DD'
@@ -135,20 +142,22 @@ def get_kwargs(self, subfield, kwargs):
             kwargs['data-type'] = 'number'
             kwargs['data-step'] = 'any'
         elif subfield.type in ['QuerySelectField', 'ModelSelectField']:
+            # QuerySelectField and ModelSelectField are for relations
             kwargs['data-type'] = 'select'
 
-            choices = {}
+            choices = []
             for choice in subfield:
                 try:
-                    choices[str(choice._value())] = str(choice.label.text)
+                    choices.append({'value': text_type(choice._value()),
+                                    'text': text_type(choice.label.text)})
                 except TypeError:
-                    choices[str(choice._value())] = ""
-            kwargs['data-source'] = choices
+                    # unable to display text value
+                    choices.append({'value': text_type(choice._value()),
+                                    'text': ''})
+
+            # blank field is already included if allow_blank
+            kwargs['data-source'] = json.dumps(choices)
         else:
             raise Exception('Unsupported field type: %s' % (type(subfield),))
 
-        # for Select2, QuerySelectField, and ModelSelectField
-        if getattr(subfield, 'allow_blank', False):
-            kwargs['data-source']['__None'] = ""
-
         return kwargs


Test patch: