flask-admin

AssertionError: A blueprint's name collision occurred

好久不见. 提交于 2019-12-11 18:57:19
问题 I'm trying to extend the flask-base project https://github.com/hack4impact/flask-base/tree/master/app. This uses the the application factory pattern in app/init.py and blueprints. I'm struggling to get the most basic functionality working so now I'm trying to follow https://flask-admin.readthedocs.io/en/v1.1.0/_sources/quickstart.txt In the app/init.py I have: from flask_admin import Admin .... adm = Admin(name='admin2') def create_app(config_name): app = Flask(__name__) app.config.from

Search on non-text column in flask-admin

橙三吉。 提交于 2019-12-11 12:12:41
问题 I have a sqlalchemy model: class Multicast(db.Model): __tablename__ = "multicast" id = Column(Integer, primary_key=True) addr = Column(Inet) name = Column(Unicode(65)) I need to make search by "addr" field. I could not do it in this way: class MulticastView(ModelView): column_searchable_list = ('name', 'addr',) column_filters = ('name', ) model = Multicast Because i had an Exception: Can only search on text columns. Failed to setup search for "addr". How can i make this search? 回答1: I found

Flask Admin extend “with select”-dropdown menu with custom button

落爺英雄遲暮 提交于 2019-12-11 00:56:27
问题 Im using the built-in view of flask admin. As you can see in the picture below: What Im trying is simple: I just want to extend the dropdown menu with a custom button. This button should perfome some action on all selected items. Is there are built-in function of flask where i can simple add an action button? 回答1: Use the @action decorator. Simple example below, the text "Recalculate Charges" is what appears in the drop-down menu. class TransactionView(AdminView): @action('recalculate',

Flask-Admin can_create = True only for a given user

白昼怎懂夜的黑 提交于 2019-12-10 18:52:53
问题 I am using Flask-Admin in conjunction with Flask-Login and mongoengine. I wish to customize views depending on users. Hereafter, an example with can_create , that allows model creation. class MyModelView(ModelView): column_exclude_list = ['password'] def is_accessible(self): if (login.current_user.login != 'admin'): can_create=False return login.current_user.is_authenticated() Such a piece of code has no effect: all users can still create, no difference between admin and non-admin users.

Flask-Admin with additional field in relationship Many to Many

*爱你&永不变心* 提交于 2019-12-10 18:19:07
问题 I have two tables: "Product", "Ingredient" and "ProductIngredient" class ProductIngredient(db.Model): __tablename__ = "product_ingredient" id = db.Column(db.Integer(), primary_key=True) product_id = db.Column('product_id', db.Integer, db.ForeignKey('product.id')) ingredient_id = db.Column('ingredient_id', db.Integer, db.ForeignKey('ingredient.id')) amount = db.Column(db.DECIMAL(10, 3)) class Ingredient(db.Model): __tablename__ = "ingredient" id = db.Column(db.Integer, primary_key=True) name =

How to enforce constraints in `flask-admin`?

点点圈 提交于 2019-12-10 11:38:51
问题 We're using http://flask-admin.readthedocs.org/ for a quick admin interface. Our model has constraints defined as follows: __table_args__ = ( db.UniqueConstraint(user_id, role, domain_id), db.UniqueConstraint(user_id, role, customer_id), ) When saving a record that violates a constraint while in debug mode, the app stops with a traceback. If not in debug mode, it reports the error in a flash message and rolls back the transaction. This is the desired behaviour (i.e. flash message and rollback

Flask-Admin: How to change model using on_model_change?

雨燕双飞 提交于 2019-12-10 10:48:45
问题 I'm trying to set a field's value to be based on another field in the same form: def on_model_change(form, model, is_created): model.textcolumn.data = model.textcolumn2.data Updating via the Flask-Admin interface raises no exceptions, but no changes were made to the value in model.textcolumn. Inspecting the "model" object, I also noticed this is not the same as the SQLAlchemy model used to generate the ModelView. How can I change model.textcolumn's value to model.textcolumn2's value? Is there

How to handle ordered many-to-many relationship (association proxy) in Flask-Admin form?

有些话、适合烂在心里 提交于 2019-12-09 13:15:33
问题 I have a many-to-many relationship between declarative models Page and Survey , which is mediated by association proxies because the order in which pages appear in a survey is important, so the cross-linking table has an additional field. from flask.ext.sqlalchemy import SQLAlchemy from sqlalchemy.ext.associationproxy import association_proxy db = SQLAlchemy() class Page (db.Model): id = db.Column(db.Integer, primary_key = True) surveys = association_proxy('page_surveys', 'survey') class

on_form_prefill delete all fields values if it executes form.process

≯℡__Kan透↙ 提交于 2019-12-09 03:40:29
I want to override the form of the Matriline model shown below. First I add an extra field clan , and in on_form_prefill I reset this field's choices and default (just testing). Without calling form.process the choices are set to my new options, but the default option is not selected. If calling form.process the choices are set to my new options and the default option is selected, but all the other fields are deleted. Anybody can help? 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()]

Dynamic SelectField validation fails with: “Not a valid choice”

亡梦爱人 提交于 2019-12-08 14:17:31
Here's my code, i can't get past the "not a valid choice" on the SelectField, being it in the creation form or the editing one... The categories i'm passing it as choices are unicode, even so i tried various "coerce" settings in the Form SelectField class ProductsView(MyModelView): create_template = '/admin/edit-products.html' form = ProductForm def create_form(self, model=None): form = self.form() choices = list(db.db.categories.find()) choices.sort(key=lambda x: x['order']) sorted_choices = [(str(cat['name']), cat['name']) for cat in choices] print sorted_choices form.category.choices =