Flask-admin,编辑关系给我外键对象的对象表示 [英] Flask-admin, editing relationship giving me object representation of Foreign Key object

查看:438
本文介绍了Flask-admin,编辑关系给我外键对象的对象表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

所需表格的SqlAlchemy模式。

 导入日期时间
从sqlalchemy.ext.declarative导入sqlalchemy
导入declarative_base
from sqlalchemy .orm import backref,relationship

Base = declarative_base()

$ b $ 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))
$ b $ def __unicode __(self):
return self.name

class Drive (Base):

编辑站中的驱动器

__tablename__ ='drives'

id = sqlalchemy。列(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(s qlalchemy.DateTime)
current_workgroup_id = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey(Workgroup.id)

workgroup = relationship('Workgroup',backref =' drive')



Admin测试



<$ p $ 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#可选,但我喜欢在列表中看到
column_hide_backrefs = False
column_list =('id','name','shorthand')


#创建管理员
admin = admin.Admin(app,name ='示例:SQLAlchemy2', (WorkgroupAdmin(schema.Workgroup,db))
admin.add_view(DriveAdmin(schema.Drive,db))

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

生成的管理表单看起来像这样。



如何获取workgroup.name值而不是对象表示?



感谢您的阅读!

解决方案

您需要获取工作组类通过 repr 函数返回其名称。

$ p $ 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)


I have a flask project, and I am getting started learning the flask-admin module.

SqlAlchemy schema for the required tables.

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')

Admin Test

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))

replacing form columns for 'workgroup' with 'workgroup.name' gives me an invalid model property name, even though I have successfully used schema.workgroup.name elsewhere in code.

The resulting admin form looks like this.

How do I go about getting the workgroup.name value to appear as opposed to the object representation?

Thanks for reading!

解决方案

You need to get the workgroup class to return its name via the repr function. That way it will show in the field.

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)

这篇关于Flask-admin,编辑关系给我外键对象的对象表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆