我对使用“ ModelView”类有疑问。 我有2个具有多对多关系的模型。我不得不修改中间表,因为它需要一个额外的元素。
我一直在寻找几种解决方案,但没有一种有效。这是我的模型,以及在视图中构建下拉菜单的尝试。
我一直在寻找很多东西,但是对于这种关系和展示,确实没有任何例子,也没有帮助。
如果您有指导我的想法,这里是我的代码示例。那太好了。预先谢谢你
models.py:
from flask_sqlalchemy import SQLAlchemy
# Create database connection object
db = SQLAlchemy()
class Cocktail(db.Model):
__tablename__ = 'cocktail'
idCocktails = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
description = db.Column(db.Text, nullable=False)
bottles = db.relationship('Bottles_has_cocktails',lazy='dynamic', cascade="all, delete-orphan", backref='cocktail_join')
path = db.Column(db.String(128))
class Bottle(db.Model):
__tablename__ = 'bottle'
idBottles = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
solenoidValve = db.Column(db.Integer, nullable=False)
step = db.Column(db.Integer, nullable=False)
empty = db.Column(db.Boolean, nullable=False)
path = db.Column(db.String(128))
cocktail = db.relationship('Bottles_has_cocktails', lazy='dynamic', cascade="all, delete-orphan", backref='bottle_join')
class Bottles_has_cocktails(db.Model):
__tablename__ = 'bottlesHasCocktail'
id_bottles = db.Column(db.Integer, db.ForeignKey('bottle.idBottles'), primary_key=True)
id_cocktails = db.Column(db.Integer, db.ForeignKey('cocktail.idCocktails'), primary_key=True)
dose = db.Column(db.Integer, nullable=False)
bottle = db.relationship(Bottle, lazy='joined')
views.py:
import os
import os.path as op
from flask import Flask, url_for
from flask_admin import Admin, form
from flask_admin.contrib import sqla
from jinja2 import Markup
from sqlalchemy.event import listens_for
from engine.models import Bottle, Cocktail, Bottles_has_cocktails, db
basedir = os.path.abspath(os.path.dirname(__file__))
file_path = os.path.join(basedir, 'static', 'files')
app = Flask(__name__, static_folder='static')
app.config.from_object('config')
db.init_app(app)
with app.app_context():
db.create_all()
@app.route('/')
def hello_world():
return 'Hello World!'
#######################
# ADMINISTRATION LINES#
#######################
@listens_for(Cocktail, 'after_delete')
def del_image(mapper, connection, target):
if target.path:
# Delete image
try:
os.remove(op.join(file_path, target.path))
except OSError:
pass
# Delete thumbnail
try:
os.remove(op.join(file_path,
form.thumbgen_filename(target.path)))
except OSError:
pass
def _list_thumbnail(view, context, model, name):
if not model.path:
return 'un test'
return Markup('<img src="%s">' % url_for('static',
filename='files/{}'.format(form.thumbgen_filename(model.path))))
class CocktailAdmin(sqla.ModelView):
column_formatters = {
'path': _list_thumbnail
}
# Alternative way to contribute field is to override it completely.
# In this case, Flask-Admin won't attempt to merge various parameters for the field.
form_extra_fields = {
'path': form.ImageUploadField('Image',
base_path=file_path,
thumbnail_size=(100, 100, True),
url_relative_path='files/',
)
}
class BottleAdmin(sqla.ModelView):
column_display_pk = True
form_columns = ['name','cocktail',]
class BottleAsCocktail(sqla.ModelView):
#column_display_pk = True
form_columns = ['dose']
admin = Admin(app, template_mode='bootstrap3')
admin.add_view(CocktailAdmin(Cocktail, db.session))
admin.add_view(BottleAdmin(Bottle, db.session))
admin.add_view(BottleAsCocktail(Bottles_has_cocktails, db.session))
if __name__ == "__main__":
app.run(host='192.168.1.81', port=5000)