英文:
Flask-Admin is there a way to append a column to the column_list rather than list them all out?
问题
如果我有一个具有数十个字段的模型,我想要在列表视图中添加一个计算属性,而不必拼写模型中自动生成的所有列,然后通过手动指定白名单来添加我想要附加的列,是否有一种方法可以让column_list
执行其正常的默认行为,只是将列追加到列表中?
所以,假设我有一个像下面这样的SQLAlchemy模型...
class Location(Base):
__tablename__ = "locations"
id: Mapped[int] = mapped_column(Sequence("location_id_seq", start=1, cycle=True), primary_key=True)
name: Mapped[str]
loc_tracking: Mapped[str] = mapped_column(default='Tagged')
loc_type: Mapped[str] = mapped_column(default='FIFO')
status: Mapped[str] = mapped_column(default='UnLocked')
allocatable: Mapped[bool] = mapped_column(default=True)
capacity: Mapped[int] = mapped_column(default=1)
# 需要在这里放入当前体积...是的,它是反规范化的,但将会被上架算法一直使用
# 并且每次进行SQL操作都会太昂贵。您还需要确保核心库存的添加/删除库存处理更新此值。
put_sequence: Mapped[int] = mapped_column(default=0)
pick_sequence: Mapped[int] = mapped_column(default=0)
stage_in_location_id: Mapped[int] = mapped_column(ForeignKey("locations.id"), nullable=True)
stage_out_location_id: Mapped[int] = mapped_column(ForeignKey("locations.id"), nullable=True)
zone_id: Mapped[int] = mapped_column(ForeignKey("zones.id"))
zone: Mapped["Zone"] = relationship(back_populates="locations")
@property
def site_name(self) -> str:
return self.zone.site_name
如果我创建一个自定义的ModelView
类,我不想手动列出所有这些列。我想要能够做类似以下的事情:
class LocationView(ModelView):
additional_columns = ('site_name',)
exclude_columns = ('capacity',)
并使其执行其正常的模型内省并将'site_name'添加到列中。
我似乎无法弄清楚我需要重写哪个方法来实现这一点。对于表单列,做类似的事情会很好。
英文:
If I have a model with dozens of fields and all I want to do is add a computed property to the list view, having to spell-out all of the otherwise automatically generated columns from the model and then add the one I want to append by manually specifying everything in a whitelist via column_list...is there a way to make column_list do it's normal default behavior and just append a column to the list?
So say I have a SQLAlchemy model like so...
class Location(Base):
__tablename__ = "locations"
id: Mapped[int] = mapped_column(Sequence("location_id_seq", start=1, cycle=True), primary_key=True)
name: Mapped[str]
loc_tracking: Mapped[str] = mapped_column(default='Tagged')
loc_type: Mapped[str] = mapped_column(default='FIFO')
status: Mapped[str] = mapped_column(default='UnLocked')
allocatable: Mapped[bool] = mapped_column(default=True)
capacity: Mapped[int] = mapped_column(default=1)
#need to put current volume here...yeah it's denormalized but will be used by putaway algs all the time
#and it'll be too expensive to make it do the SQL every time. You'll also need to make sure add/remove inv
#from core.inventory deals with updating this value.
put_sequence: Mapped[int] = mapped_column(default=0)
pick_sequence: Mapped[int] = mapped_column(default=0)
stage_in_location_id: Mapped[int] = mapped_column(ForeignKey("locations.id"), nullable=True)
stage_out_location_id: Mapped[int] = mapped_column(ForeignKey("locations.id"), nullable=True)
zone_id: Mapped[int] = mapped_column(ForeignKey("zones.id"))
zone: Mapped["Zone"] = relationship(back_populates="locations")
@property
def site_name(self) -> str:
return self.zone.site_name
If I make a custom ModelView class, I don't want to have to manually list out all of those columns. I'd like to be able to do something like:
class LocationView(ModelView):
additional_columns = ('site_name',)
exclude_columns = ('capacity',)
And have it do its normal model introspection and add 'site_name' to the list columns.
I can't seem to figure out what method I need to override to accomplish this. It'd be nice to do something similar for form columns.
答案1
得分: 1
你可以将column_list
定义为一个属性,并利用方法scaffold_list_columns()
返回除id列之外的所有列。注意,如果在视图中未定义column_list
,则会调用column_list
。
例如:
class LocationView(ModelView):
@property
def column_list(self):
return self.scaffold_list_columns() + ['site_name']
exclude_columns = ('capacity',)
英文:
You can define column_list
as a property and use the fact that the method scaffold_list_columns()
returns all the columns except the id column. Note, scaffold_list_columns()
is called if column_list
is not defined in the view.
For example:
class LocationView(ModelView):
@property
def column_list(self):
return self.scaffold_list_columns() + ['site_name']
exclude_columns = ('capacity',)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论