gpt4 book ai didi

python - Flask-admin,编辑关系给我外键对象的对象表示

转载 作者:太空宇宙 更新时间:2023-11-03 14:15:32 25 4
gpt4 key购买 nike

我有一个 flask 项目,我正在开始学习 flask-admin 模块。

所需表的 SqlAlchemy 架构。

import datetime
import sqlalchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship

Base = declarative_base()


class Workgroup(Base):
__tablename__ = 'workgroups'
id = sqlalchemy.Column(sqlalchemy.Integer,

primary_key=True,
autoincrement=True
)
name = sqlalchemy.Column(sqlalchemy.String(16))
shorthand = sqlalchemy.Column(sqlalchemy.String(4))

def __unicode__(self):
return self.name

class Drive(Base):
"""
A drive in an edit station.
"""
__tablename__ = 'drives'

id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True,
autoincrement=True
)
name = sqlalchemy.Column(sqlalchemy.String(64))
computer_id = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey(Computer.id)
)
computer = relationship('Computer', backref='drives')
is_active = sqlalchemy.Column(sqlalchemy.Boolean)
free_space = sqlalchemy.Column(sqlalchemy.BigInteger)
used_space = sqlalchemy.Column(sqlalchemy.BigInteger)
total_space = sqlalchemy.Column(sqlalchemy.BigInteger)
percentage_full = sqlalchemy.Column(sqlalchemy.Float)
boot_time = sqlalchemy.Column(sqlalchemy.DateTime)

last_changed_workgroup = sqlalchemy.Column(sqlalchemy.DateTime)
last_checked_in = sqlalchemy.Column(sqlalchemy.DateTime)
last_notified = sqlalchemy.Column(sqlalchemy.DateTime)
image_version = sqlalchemy.Column(sqlalchemy.String(64))
image_date = sqlalchemy.Column(sqlalchemy.DateTime)
current_workgroup_id = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey(Workgroup.id)
)
workgroup = relationship('Workgroup', backref='drives')

管理测试

class DriveAdmin(sqla.ModelView):
column_display_pk = True
column_hide_backrefs = False
column_display_all_relations = True
form_columns = [ 'computer_id', 'workgroup.name', ]
column_list = ('computer.name', 'name', 'workgroup', 'computer.short_description', 'computer.notes',
'computer.station_type.description', 'computer.room.name')


class WorkgroupAdmin(sqla.ModelView):
column_display_pk = True # optional, but I like to see the IDs in the list
column_hide_backrefs = False
column_list = ('id', 'name', 'shorthand')


# Create admin
admin = admin.Admin(app, name='Example: SQLAlchemy2', template_mode='bootstrap3')
admin.add_view(WorkgroupAdmin(schema.Workgroup, db))
admin.add_view(DriveAdmin(schema.Drive, db))

用“workgroup.name”替换“workgroup”的表单列会给我一个无效的模型属性名称,即使我已经在代码的其他地方成功使用了 schema.workgroup.name。

生成的管理表单如下所示。 enter image description here

如何让 workgroup.name 值出现而不是对象表示?

感谢阅读!

最佳答案

您需要通过repr 函数让工作组类返回其名称。这样它就会显示在现场。

class Workgroup(Base):
__tablename__ = 'workgroups'
id = sqlalchemy.Column(sqlalchemy.Integer,

primary_key=True,
autoincrement=True
)
name = sqlalchemy.Column(sqlalchemy.String(16))
shorthand = sqlalchemy.Column(sqlalchemy.String(4))

def __unicode__(self):
return self.name

def __repr__(self):
return '<Workgroup %r>' % (self.name)

关于python - Flask-admin,编辑关系给我外键对象的对象表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33698104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com