flask-admin

How do you add a summary row for Flask-Admin?

泪湿孤枕 提交于 2019-12-20 03:24:04
问题 In my flask-admin index_view, I am displaying financial information for my rows. I would like to add an extra row, "summary row", at the bottom of my index_view table which sums up all the columns. How can I accomplish this? 回答1: There's a couple of things you need to do. Provide a custom list.html template and override the render() method for the view. In the render method inject your summary data into the kwargs and in the custom template use the summary data to output appropriate html. You

Hiding fields in Flask-Admin depending on logged in user?

一个人想着一个人 提交于 2019-12-19 04:21:44
问题 I have Users and Roles in my Flask app thanks to Flask-Security. For some roles I would like to hide certain fields in the forms created by Flask-Admin. I know about customizing ModelViews with eg. form_create_rules = ('title', 'file') but while instantiating a ModelView there isn't access to the current request so current_user.has_role(USER_ROLE) can't be called. Is there any other way to achieve this? 回答1: One way of achieving this is to create multiple view classes and register these view

flask admin : sqlalchemy.exc.InterfaceError(Error binding parameter 8)

时光怂恿深爱的人放手 提交于 2019-12-18 09:51:18
问题 I was trying to save a project's reviewer using below, and the select field shows correct: # Query the user with Role.id == 4 as reviewer def reviewer_choices(): return User.query.join(User.roles).filter(Role.id == 4) # Build a select field class ProjectView(sqla.ModelView): form_extra_fields = { 'reviewer': sqla.fields.QuerySelectField( label='Reviewer', query_factory=reviewer_choices, )} However, when I was trying to save it, the error occurred: InterfaceError: (sqlite3.InterfaceError)

flask-admin form: Constrain Value of Field 2 depending on Value of Field 1

吃可爱长大的小学妹 提交于 2019-12-17 18:40:17
问题 One feature I have been struggling to implement in flask-admin is when the user edits a form, to constrain the value of Field 2 once Field 1 has been set. Let me give a simplified example in words (the actual use case is more convoluted). Then I will show a full gist that implements that example, minus the "constrain" feature. Let's say we have a database that tracks some software "recipes" to output reports in various formats. The recipe table of our sample database has two recipes: "Serious

Tyring to set up modelview with Flask-Admin causes ImportError

拥有回忆 提交于 2019-12-17 06:18:05
问题 I am trying to add a User model view to Flask-Admin. However, I get ImportError: cannot import name db . Why is this happening and how do I fix it? app/__init__.py : from flask import Flask from flask_sqlalchemy import SQLAlchemy import flask_admin as admin from app.models import User db = SQLAlchemy() admin = admin.Admin(name="Admin Panel") def create_app(config_name): app = Flask(__name__) db.init_app(app) admin.init_app(app) admin.add_view(ModelView(User, db.session)) return app app/models

ImportError: cannot import name 'db'

ぐ巨炮叔叔 提交于 2019-12-13 02:47:05
问题 I am working on adding flask admin to a preexisting flask boiler plate project. I've been able to get the basic project working at https://github.com/kc1/flask-base (SCREENSHOT). I now need to add modelviews to add basic CRUD functionality. To do this I changed the code to : adm = Admin(app,name='flaskadmin') from app.models import User adm.add_view(ModelView(User, db.session)) You can see that it works. But if I import the User model with the rest of the imports at the top of app/init I get:

How to set the value of an extra field in the list view?

五迷三道 提交于 2019-12-13 01:16:42
问题 I added an extra field clan to the ModelView of the class Matriline and add it to the columns in list view, as shown below. How can I set the value of the extra field clan in the list view? views.py class MatrilineAdmin(sqla.ModelView): form_extra_fields = { 'clan': SelectField('Clan', coerce=int, choices=[ (c.id, c.name) for c in Clan.query.all()]) } create_template = 'admin/create.html' edit_template = 'admin/edit.html' column_list = ('name', 'pod', 'clan') models.py class Matriline(db

Flask-admin create a button next to a text field

邮差的信 提交于 2019-12-12 12:03:53
问题 I've made a simple form using Flask-admin that looks like the following. I'm now looking into creating a refresh button next to the API text field, but don't really now how to. Is this called a widget or inline model? I haven't been able to find a button in WTForms either that I can use. Would appreciate any examples on this as I've been able to override textfields, but not place other elements next to it in a easy way. class AdministratorView(sqla.ModelView): page_size = 10 column_searchable

Additional action button doesn't work on flask-admin

扶醉桌前 提交于 2019-12-12 09:55:35
问题 I'm trying to add one more action to flask-admin forms. It has to increment rating (+1) and it works with batch action, but not with single. Please help me find the bug, I've spent a lot of time trying to make this thing work properly. Here's the code: I made an html template in templates folder - custom_lists.html {% extends 'admin/model/list.html' %} {% block list_row_actions %} {{ super() }} <form class="icon" method="POST" action="/admin/user/action/"> <input id="action" name="action"

How to customize flask admin QuerySelectMultipleField choice?

我的梦境 提交于 2019-12-12 04:16:40
问题 I am using flask_admin to allow admin user to access database, where one-to-many relationship is presented. When editing an entry, I would like the dropdown menu to show only the options that meet the condition. I though query_factory can do this. The following is minimal example of what I have now: class OneSideObj(db.Model): id = db.Column(db.Integer, primary_key=True) active = db.Column(db.Boolean) many_side_obj_id = db.Column( db.Integer, db.ForeignKey('many_side_obj.id') ) class