Commit e17f82de authored by Petrus J.v.Rensburg's avatar Petrus J.v.Rensburg

Getting Started: Initialization & Adding model views.

parent 61005daf
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
...@@ -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
------------------------- -------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment