- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我修改了基类以包含我的所有表都具有的三个默认列:
class Base(object):
id = Column(Integer, primary_key=True)
date_created = Column(DateTime, default=func.current_timestamp())
date_modified = Column(DateTime, default=func.current_timestamp(),
onupdate=func.current_timestamp())
我在两列之间有一个一对多的关系:
class User(Base):
__tablename__ = 'users'
name = Column(Text)
password = Column(Text)
items = relationship("Item", backref=
backref('user', order_by=date_modified),
cascade="all, delete, delete-orphan")
class Item(Base):
__tablename__ = 'items'
user_id = Column(Integer, ForeignKey('users.id'))
title = Column(Text)
如果我在每个表的类中明确定义了 date_created 和 date_modified 列,这过去工作得很好。但是,当从 Base 继承时,它不起作用并且出现以下错误:
NameError: name 'date_modified' is not defined
如何使用 order_by=column_from_mixin
(order_by=date_modified
) 对 backref 关系进行排序?
谢谢。
最佳答案
您可以使用其中任何一个:
backref('user', order_by=lambda: User.date_modified)
backref('user', order_by='User.date_modified')
可以使用类名 User.date_modified
访问类属性,但此时类本身仍未定义。提供可调用(第二种情况在内部转换为可调用)推迟名称解析,直到首次使用映射,此时所有映射类都已定义。
关于python - 如何在 SQLAlchemy 中使用来自 backref(.., order_by=..) 内部混合类的列名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15053649/
如何根据多对多关系条件是否为真来对查询集进行排序? 即 class Publication(models.Model): name = models.CharField(max_length=
我想获取 MyModel order_by('-end_date') 的最后 100 条记录,并对不同的获胜者类型进行 SUM 注释 MyModel.objects.all()[:100].order
我想根据两个因素对 QuerySet 进行排序。 A 和 B,但它应该将 A 优先于 B。也就是说,它应该对 A 进行排序,但是如果 A 对 QuerySet 中的两个对象相等,则它应该对因子 B 对
重复: Using a Django custom model method property in order_by() 我有两个模型;一个存储帖子,另一个存储对这些帖子进行的投票,使用外键字段进行
我在模型 roll_numb 中有字段。 roll_numb 的值如下。 070-001 070-007 070-343 080-002 080-008 当我 order_by roll_numb 时
所以我在使用order_by函数的查询中使用preload遇到麻烦。 通常我在order_by函数中使用list_member list_member def list_members() do qu
我们有一个复杂的查询,它使用跨表和 order_by 的联接。 示例如下: select distinct `accounts`.`id`, `accounts`.`number_of_lis
我有一个这样的数据库 order_id | date | qty ------------------------------------ a |2018-1
假设我有一列包含 1-100 之间的浮点值。 我希望能够将该值转换为 1-10 之间精度较低的整数,然后根据这个新值对结果进行排序。 想要降低排序的精确度似乎很奇怪,但 SQL 语句按 2 列排序,如
我有范围内的距离数据,例如 5-10、0-5、10-15、 我正在使用此查询来按排序顺序获取数据: $this->db->select("distance_from_mall"); $this->db
我必须对包含主机名的对象列表进行排序。 主机名采用以下格式:h1、h5、h10、h12、h12-abc、h1000、x10 如果我使用 order_by('hostname') 它会这样排序: h1,
我在 MySQL 数据库中有一系列“编号”的 varchar 字段: +-----------+ | name | +-----------+ | thing 1 | | thing
我不想运行查询来获取所有超过 6 个 com 的文章,然后根据 com 列表的长度进行排序,为此,我这样做了: ArticleModel.objects.filter(com__6__exists=
我有一个模型 Line,其中包含一个 total_value 和一个 group 字段。我使用以下代码获取给定组中具有最高值的 10 行: group_lines = Line.objects.fil
已阅读 django docs on order_by 有一个注释/警告(如果我理解正确的话)说: 如果您使用多值字段对查询集进行排序,则该查询集中具有多个相关项的每个元素都将多次添加到由 order
我正在使用 Django 1.7.7。 我想知道是否有人经历过这种情况。这是我的查询: events = Event.objects.filter( Q(date__gt=my_date) |
我在我的 template.php 文件中使用查询来显示 wooCommerce按类别分类的产品。 我的查询如下所示: 'product', 'stock' => 1, 'pos
我处于必须通过用于存储街道地址的 CharField 输出相当大的对象列表的情况。 我的问题是,显然数据是按 ASCII 代码排序的,因为它是一个 Charfield,具有可预测的结果......它像
Foo.objects.all().distinct('foo')不会删除重复项 Foo.objects.all().order_by().distinct('foo')确实删除它们。 我有 Meta
我喜欢 Django,喜欢它实现的分页功能。但是,我在尝试跨多个页面拆分随机排序的查询集时遇到了问题。 例如,我在查询集中有 100 个元素,并希望一次显示 25 个。将上下文对象作为随机排序的查询集
我是一名优秀的程序员,十分优秀!