0
# Flask-Admin
1
2
Flask-Admin is a simple and extensible admin interface framework for Flask applications. It provides a flexible system for creating administrative interfaces with minimal code, supporting multiple ORMs including SQLAlchemy, MongoEngine, and Peewee, along with file management capabilities.
3
4
## Package Information
5
6
- **Package Name**: flask-admin
7
- **Language**: Python
8
- **Installation**: `pip install flask-admin`
9
10
## Core Imports
11
12
```python
13
from flask_admin import Admin, BaseView, AdminIndexView, expose
14
```
15
16
For model views with specific ORMs:
17
18
```python
19
from flask_admin.contrib.sqla import ModelView as SQLAModelView
20
from flask_admin.contrib.mongoengine import ModelView as MongoModelView
21
from flask_admin.contrib.fileadmin import FileAdmin
22
```
23
24
For actions and menu items:
25
26
```python
27
from flask_admin.actions import action
28
from flask_admin.menu import MenuLink
29
```
30
31
## Basic Usage
32
33
```python
34
from flask import Flask
35
from flask_admin import Admin, BaseView, expose
36
from flask_admin.contrib.sqla import ModelView
37
from flask_sqlalchemy import SQLAlchemy
38
39
app = Flask(__name__)
40
app.config['SECRET_KEY'] = 'your-secret-key'
41
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///admin.db'
42
43
db = SQLAlchemy(app)
44
45
# Sample model
46
class User(db.Model):
47
id = db.Column(db.Integer, primary_key=True)
48
name = db.Column(db.String(100))
49
email = db.Column(db.String(120), unique=True)
50
51
# Initialize admin
52
admin = Admin(app, name='My Admin', template_mode='bootstrap3')
53
54
# Add model view
55
admin.add_view(ModelView(User, db.session, name='Users'))
56
57
# Custom view
58
class CustomView(BaseView):
59
@expose('/')
60
def index(self):
61
return self.render('custom_template.html')
62
63
admin.add_view(CustomView(name='Custom Page', category='Tools'))
64
65
if __name__ == '__main__':
66
with app.app_context():
67
db.create_all()
68
app.run(debug=True)
69
```
70
71
## Architecture
72
73
Flask-Admin follows a modular architecture built around several key concepts:
74
75
- **Admin Collection**: The main `Admin` class manages views and creates the admin interface
76
- **Views**: Individual admin pages, either custom (`BaseView`) or model-based (`ModelView`)
77
- **Menu System**: Hierarchical navigation with categories, links, and automatic view registration
78
- **ORM Integration**: Contrib modules provide seamless integration with popular ORMs
79
- **Template System**: Jinja2 templates with customizable themes and layouts
80
- **Security**: Built-in permission checking and CSRF protection
81
82
## Capabilities
83
84
### Core Admin Interface
85
86
Main admin interface management including view registration, menu organization, and Flask application integration.
87
88
```python { .api }
89
class Admin:
90
def __init__(
91
self,
92
app=None,
93
name=None,
94
url=None,
95
subdomain=None,
96
index_view=None,
97
translations_path=None,
98
endpoint=None,
99
static_url_path=None,
100
base_template=None,
101
template_mode=None,
102
category_icon_classes=None
103
): ...
104
105
def add_view(self, view): ...
106
def add_views(self, *args): ...
107
def init_app(self, app, index_view=None, endpoint=None, url=None): ...
108
```
109
110
[Core Admin Interface](./core-admin.md)
111
112
### Base Views and Custom Views
113
114
Foundation classes for creating custom admin views with routing, templating, and permission controls.
115
116
```python { .api }
117
@expose(url='/', methods=('GET',))
118
def expose_decorator(url='/', methods=('GET',)): ...
119
120
class BaseView:
121
def __init__(
122
self,
123
name=None,
124
category=None,
125
endpoint=None,
126
url=None,
127
static_folder=None,
128
static_url_path=None,
129
menu_class_name=None,
130
menu_icon_type=None,
131
menu_icon_value=None
132
): ...
133
134
def is_accessible(self): ...
135
def render(self, template, **kwargs): ...
136
```
137
138
[Base Views and Custom Views](./core-admin.md)
139
140
### Model Views and CRUD Operations
141
142
Base model view functionality for database-driven admin interfaces with list, create, edit, delete operations.
143
144
```python { .api }
145
class BaseModelView:
146
# Configuration attributes
147
list_columns = None
148
column_searchable_list = None
149
column_filters = None
150
form_columns = None
151
can_create = True
152
can_edit = True
153
can_delete = True
154
can_export = False
155
export_types = ['csv']
156
```
157
158
[Model Views and CRUD Operations](./model-views.md)
159
160
### Forms and Field Types
161
162
Advanced form system with custom field types, validation, and widget support for admin interfaces.
163
164
```python { .api }
165
class BaseForm: ...
166
class SecureForm: ...
167
168
class DateTimeField: ...
169
class TimeField: ...
170
class Select2Field: ...
171
class Select2TagsField: ...
172
class JSONField: ...
173
```
174
175
[Forms and Field Types](./forms.md)
176
177
### SQLAlchemy Integration
178
179
Complete SQLAlchemy ORM integration providing database model administration with relationships, filtering, and advanced querying.
180
181
```python { .api }
182
from flask_admin.contrib.sqla import ModelView
183
184
class ModelView(BaseModelView):
185
def __init__(self, model, session, **kwargs): ...
186
```
187
188
[SQLAlchemy Integration](./sqlalchemy-integration.md)
189
190
### MongoEngine Integration
191
192
MongoDB document administration through MongoEngine ORM integration with embedded documents and NoSQL-specific features.
193
194
```python { .api }
195
from flask_admin.contrib.mongoengine import ModelView, EmbeddedForm
196
197
class ModelView(BaseModelView): ...
198
class EmbeddedForm: ...
199
```
200
201
[MongoEngine Integration](./mongoengine-integration.md)
202
203
### File Management
204
205
File and directory administration interface with upload, download, edit, and file browser capabilities.
206
207
```python { .api }
208
from flask_admin.contrib.fileadmin import FileAdmin, LocalFileStorage
209
210
class FileAdmin(BaseFileAdmin):
211
def __init__(self, base_path, *args, **kwargs): ...
212
213
class LocalFileStorage:
214
def __init__(self, base_path): ...
215
```
216
217
[File Management](./file-admin.md)
218
219
### Actions and Batch Operations
220
221
Batch action system for performing operations on multiple selected items in model views.
222
223
```python { .api }
224
@action(name, text, confirmation=None)
225
def action_decorator(name, text, confirmation=None): ...
226
227
class ActionsMixin:
228
def handle_action(self, return_view=None): ...
229
def is_action_allowed(self, name): ...
230
```
231
232
[Actions and Batch Operations](./model-views.md)
233
234
### Helper Functions and Utilities
235
236
Utility functions for form handling, URL generation, security, and template context management.
237
238
```python { .api }
239
def get_current_view(): ...
240
def is_safe_url(target): ...
241
def flash_errors(form, message): ...
242
def prettify_class_name(name): ...
243
```
244
245
[Helper Functions and Utilities](./helpers-utilities.md)
246
247
### Redis CLI Integration
248
249
Interactive Redis console providing web-based command execution for Redis database administration.
250
251
```python { .api }
252
from flask_admin.contrib.rediscli import RedisCli
253
254
class RedisCli(BaseView):
255
def __init__(self, redis, name=None, category=None, endpoint=None, url=None): ...
256
```
257
258
[Redis CLI Integration](./redis-integration.md)
259
260
### GeoAlchemy Integration
261
262
Geographic data support through GeoAlchemy2 integration with interactive map widgets for spatial database administration.
263
264
```python { .api }
265
from flask_admin.contrib.geoa import ModelView
266
from flask_admin.contrib.geoa.fields import GeoJSONField
267
from flask_admin.contrib.geoa.widgets import LeafletWidget
268
269
class ModelView(SQLAModelView): ...
270
class GeoJSONField(JSONField): ...
271
class LeafletWidget(TextArea): ...
272
```
273
274
[GeoAlchemy Integration](./geoalchemy-integration.md)
275
276
### Google App Engine Integration
277
278
Google App Engine datastore integration supporting both legacy DB API and modern NDB API models.
279
280
```python { .api }
281
from flask_admin.contrib.appengine import ModelView
282
from flask_admin.contrib.appengine.view import NdbModelView, DbModelView
283
284
def ModelView(model): ...
285
class NdbModelView(BaseModelView): ...
286
class DbModelView(BaseModelView): ...
287
```
288
289
[Google App Engine Integration](./appengine-integration.md)
290
291
### Menu System and Navigation
292
293
Flexible menu system supporting categories, links, icons, and hierarchical navigation structures.
294
295
```python { .api }
296
class MenuLink:
297
def __init__(
298
self,
299
name,
300
url=None,
301
endpoint=None,
302
category=None,
303
class_name=None,
304
icon_type=None,
305
icon_value=None,
306
target=None
307
): ...
308
```
309
310
[Menu System and Navigation](./core-admin.md)