Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
flask-admin
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Python-Dev
flask-admin
Commits
e17f82de
Commit
e17f82de
authored
Jun 26, 2015
by
Petrus J.v.Rensburg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Getting Started: Initialization & Adding model views.
parent
61005daf
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
74 deletions
+72
-74
customising_builtin_views.rst
doc/customising_builtin_views.rst
+55
-6
getting_started.rst
doc/getting_started.rst
+17
-68
No files found.
doc/customising_builtin_views.rst
View file @
e17f82de
Customising builtin views
.. _customising-builtin-views:
===========
Customising Builtin Views
=================================
Image fields
Image fields
-----------
-----------
----
HTML fields
HTML fields
-----------
-----------
----
List view options
List view options
---------------
---------------
----
Form view options
Form view options
---------------
-------------------
To customize these model views, you have two options: Either you can override the public properties of the *ModelView*
class, or you can override its methods.
For example, if you want to disable model creation and only show certain columns in the list view, you can do
something like::
from flask_admin.contrib.sqla import ModelView
# Flask and Flask-SQLAlchemy initialization here
class MyView(ModelView):
# Disable model creation
can_create = False
# Override displayed fields
column_list = ('login', 'email')
def __init__(self, session, **kwargs):
# You can pass name and other parameters if you want to
super(MyView, self).__init__(User, session, **kwargs)
admin = Admin(app)
admin.add_view(MyView(db.session))
Overriding form elements can be a bit trickier, but it is still possible. Here's an example of
how to set up a form that includes a column named *status* that allows only predefined values and
therefore should use a *SelectField*::
from wtforms.fields import SelectField
class MyView(ModelView):
form_overrides = dict(status=SelectField)
form_args = dict(
# Pass the choices to the `SelectField`
status=dict(
choices=[(0, 'waiting'), (1, 'in_progress'), (2, 'finished')]
))
It is relatively easy to add support for different database backends (Mongo, etc) by inheriting from
:class:`~flask_admin.model.BaseModelView`.
class and implementing database-related methods.
Please refer to :mod:`flask_admin.contrib.sqla` documentation on how to customize the behavior of model-based
administrative views.
\ No newline at end of file
doc/getting_started.rst
View file @
e17f82de
...
@@ -14,38 +14,29 @@ class for each of your models.
...
@@ -14,38 +14,29 @@ class for each of your models.
Initialization
Initialization
--------------
--------------
To start using Flask-Admin, you have to create a :class:`~flask_admin.base.Admin` class instance and associate it
The first step, is to initialise an empty admin interface on your Flask app::
with the Flask
application instance::
from flask import Flask
from flask import Flask
from flask_admin import Admin
from flask_admin import Admin
app = Flask(__name__)
app = Flask(__name__)
admin = Admin(app)
admin = Admin(app
, name='My App'
)
# Add administrative views here
# Add administrative views here
app.run()
app.run()
If you start this application and navigate to `http://localhost:5000/admin/ <http://localhost:5000/admin/>`_,
Here, the *name* parameter is optional. If you start this application and navigate to `http://localhost:5000/admin/ <http://localhost:5000/admin/>`_,
you should see an empty "Home" page with a navigation bar on top
you should see an empty "Home" page with a navigation bar on top, and the *name* that you specified.
.. image:: images/quickstart/quickstart_1.png
:target: ../_images/quickstart_1.png
You can change the application name by passing a value for the *name* parameter to the
:class:`~flask_admin.base.Admin` class constructor::
admin = Admin(app, name='My App')
As an alternative to passing a Flask application object to the Admin constructor, you can also call the
.. note::
:meth:`~flask_admin.base.Admin.init_app` function, after the Admin instance has been initialized::
admin = Admin(name='My App')
As an alternative to passing a Flask application object to the Admin constructor, you can also call the
# Add views here
:meth:`~flask_admin.base.Admin.init_app` function, after the Admin instance has been initialized::
admin.init_app(app)
admin = Admin(name='My App')
# Add views here
admin.init_app(app)
Adding Model Views
Adding Model Views
-----------
-----------
...
@@ -61,57 +52,15 @@ is the SQLAlchemy backend, which you can use as follows::
...
@@ -61,57 +52,15 @@ is the SQLAlchemy backend, which you can use as follows::
admin = Admin(app)
admin = Admin(app)
admin.add_view(ModelView(User, db.session))
admin.add_view(ModelView(User, db.session))
This creates an admin page for the *User* model. By default, the list view looks like
Straight out of the box, this gives you a set of fully featured *CRUD* views for your model:
.. image:: images/quickstart/quickstart_4.png
:width: 640
:target: ../_images/quickstart_4.png
To customize these model views, you have two options: Either you can override the public properties of the *ModelView*
class, or you can override its methods.
For example, if you want to disable model creation and only show certain columns in the list view, you can do
something like::
from flask_admin.contrib.sqla import ModelView
# Flask and Flask-SQLAlchemy initialization here
class MyView(ModelView):
# Disable model creation
can_create = False
# Override displayed fields
column_list = ('login', 'email')
def __init__(self, session, **kwargs):
# You can pass name and other parameters if you want to
super(MyView, self).__init__(User, session, **kwargs)
admin = Admin(app)
admin.add_view(MyView(db.session))
Overriding form elements can be a bit trickier, but it is still possible. Here's an example of
how to set up a form that includes a column named *status* that allows only predefined values and
therefore should use a *SelectField*::
from wtforms.fields import SelectField
class MyView(ModelView):
form_overrides = dict(status=SelectField)
form_args = dict(
# Pass the choices to the `SelectField`
status=dict(
choices=[(0, 'waiting'), (1, 'in_progress'), (2, 'finished')]
))
It is relatively easy to add support for different database backends (Mongo, etc) by inheriting from
* A list view, with support for searching, sorting and filtering
:class:`~flask_admin.model.BaseModelView`.
* a view for adding new records
class and implementing database-related methods.
* a view for editing existing records
* the ability to delete records.
Please refer to :mod:`flask_admin.contrib.sqla` documentation on how to customize the behavior of model-based
There are many options available for customizing the display and functionality of these builtin view.
administrative views
.
For more details on that, see :ref:`customising-builtin-views`
.
Overriding the index page
Overriding the index page
-------------------------
-------------------------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment