From df6b5968c6267af2d8c4bc243bee17b4bd3b621b Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Sun, 15 Oct 2023 09:13:31 +0200 Subject: [PATCH] Use isinstance instead of comparing class type --- flask_admin/contrib/mongoengine/view.py | 14 +++++--------- flask_admin/contrib/peewee/view.py | 9 +++------ 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/flask_admin/contrib/mongoengine/view.py b/flask_admin/contrib/mongoengine/view.py index 10663164a..41ef0a41b 100644 --- a/flask_admin/contrib/mongoengine/view.py +++ b/flask_admin/contrib/mongoengine/view.py @@ -320,17 +320,13 @@ def scaffold_list_columns(self): columns = [] for n, f in self._get_model_fields(): - # Verify type - field_class = type(f) - - if (field_class == mongoengine.ListField and - isinstance(f.field, mongoengine.EmbeddedDocumentField)): + if isinstance(f, mongoengine.ListField) and isinstance(f.field, mongoengine.EmbeddedDocumentField): continue - if field_class == mongoengine.EmbeddedDocumentField: + if isinstance(f, mongoengine.EmbeddedDocumentField): continue - if self.column_display_pk or field_class != mongoengine.ObjectIdField: + if self.column_display_pk or not isinstance(f, mongoengine.ObjectIdField): columns.append(n) return columns @@ -343,7 +339,7 @@ def scaffold_sortable_columns(self): for n, f in self._get_model_fields(): if type(f) in SORTABLE_FIELDS: - if self.column_display_pk or type(f) != mongoengine.ObjectIdField: + if self.column_display_pk or not isinstance(f, mongoengine.ObjectIdField): columns[n] = f return columns @@ -467,7 +463,7 @@ def _search(self, query, search_term): criteria = None for field in self._search_fields: - if type(field) == mongoengine.ReferenceField: + if isinstance(field, mongoengine.ReferenceField): import re regex = re.compile('.*%s.*' % term) else: diff --git a/flask_admin/contrib/peewee/view.py b/flask_admin/contrib/peewee/view.py index ee77873a4..7b31302eb 100644 --- a/flask_admin/contrib/peewee/view.py +++ b/flask_admin/contrib/peewee/view.py @@ -194,12 +194,9 @@ def scaffold_list_columns(self): columns = [] for n, f in self._get_model_fields(): - # Verify type - field_class = type(f) - - if field_class == ForeignKeyField: + if isinstance(f, ForeignKeyField): columns.append(n) - elif self.column_display_pk or field_class != PrimaryKeyField: + elif self.column_display_pk or not isinstance(f, PrimaryKeyField): columns.append(n) return columns @@ -208,7 +205,7 @@ def scaffold_sortable_columns(self): columns = dict() for n, f in self._get_model_fields(): - if self.column_display_pk or type(f) != PrimaryKeyField: + if self.column_display_pk or not isinstance(f, PrimaryKeyField): columns[n] = f return columns