简体   繁体   English

声明式查询中的Python Pony ORM单行属性

[英]Python Pony ORM one-line property inside declarative queries

I'm trying to use one-line property inside select query. 我试图在选择查询中使用单行属性。 It should be possible due to 0.7.4 & 0.7.5 . 由于0.7.4和0.7.5应该是可能的。 Simple operation such as + (inside value_at_end property) works but I'm not able to use split() and other. + (inside value_at_end property)简单操作+ (inside value_at_end property)有效,但是我无法使用split()和其他方法。 Regarding 0.7.4 and 0.7.5 update it should be possible. 关于0.7.4和0.7.5更新应该是可能的。

class Foo(db.Entity):
    _table_ = "sample_table"
    some_string_with_value_at_end = Optional(str)

    @property
    def value_at_end(self):
        return self.some_string_with_value_at_end.split('/')[-1]

    @classmethod
    def get_values_at_end(cls):
        values = select(v.value_at_end for v in cls)
        return values

items = Foo.get_values_at_end()
for each in items:
    print(each)

Getting error: 出现错误:

AttributeError: 'str' object has no attribute 'split': self.some_string_with_value_at_end.split (inside Foo.value_at_end)

Right now I'm using raw_sql but wanted to make more python, it should be possible? 现在我正在使用raw_sql,但想制作更多的python,应该可以吗?

Thanks for help! 感谢帮助!

At this moment Pony does not know how to translate str.split method call to SQL. 目前,Pony不知道如何将str.split方法调用转换为SQL。

Theoretically it is possible, but we need to know appropriate translation for each supported database. 从理论上讲是可能的,但是我们需要知道每个受支持数据库的适当翻译。 For some of them the resulted expression may look pretty complex as in this answer 对于其中的某些人,结果表达式可能看起来很复杂,如以下答案所示

So I doubt we will add support of str.split translation in near future. 因此,我怀疑我们是否会在不久的将来增加对str.split翻译的支持。 Maybe it is easier for you to use JSON column and store array of string values in it. 也许使用JSON列并在其中存储字符串值数组更容易。 Then you can write 那你可以写

class Foo(db.Entity):
    str_items = Optional(Json)

    @property
    def value_at_end(self):
        return self.str_items[-1]

    @classmethod
    def get_values_at_end(cls):
        values = select(v.value_at_end for v in cls)
        return values

Right now using negative index to access last item of JSON array should work in PostgreSQL, and I think we should add it for other databases too, it looks much easier than to add str.split method with all possible use-cases. 现在,使用负索引访问JSON数组的最后一项在PostgreSQL中应该可以工作,而且我认为我们也应该为其他数据库添加它,这比在所有可能的用例中添加str.split方法要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM