if语句在hybrid_property表达式中

时间:2015-12-11 16:45:19

标签: python sqlalchemy conditional

我有expression执行一些条件并返回响应。我希望能够对此字段进行查询,因此我创建了@hybrid_property def func(self): if self.some_column is True: return self.true_column * self.amount return self.false_column * self.amount @func.expression def func(cls): # ??? return 。但是,我不确定如何为这种用例编写条件。

如何根据条件的结果编写一个表达式,将两个字段之一与另一个字段相乘?

"use strict";

var locale = require("locale");
module.exports = function (supportedLanguages, defaultLocale) {

    // Creation of the list of supported locales
    var supportedLocales = new locale.Locales(supportedLanguages);
    locale.Locale.default = defaultLocale;

    return function (req, res, next) {
        var matches,
            askedLocale = null,
            bestLocale = null,
            locales = null,
            url = null;
        // Test if locale in url 
        matches = req.url.match(/^\/([a-zA-Z]{2})([\/\?].*)?$/i);
        // If locale defined in url
        if (matches !== null) {
            // Get locale
            askedLocale = matches[1].toLowerCase();
            // Modify url for express logic
            req.url = matches[2] || '/';
        }
        // If the asked locale is supported
        if (askedLocale !== null && supportedLanguages.indexOf(askedLocale) !== -1) {
            locales = new locale.Locales(askedLocale);
        } else {
            // Get locale from headers
            locales = new locale.Locales(req.headers["accept-language"]);
        }
        // Getting the best supported locale
        bestLocale = locales.best(supportedLocales).toString();
        // If the asked locale is not the best supported
        if (askedLocale !== null && askedLocale !== bestLocale) {
            // Creation of the new url
            url = "https://" + req.headers.host + "/" + bestLocale + req.url;
            // Redirection to the new locale
            res.redirect(302, url);
        } else {
            // Else if already best locale
            req.setLocale(bestLocale);
            res.locals.locale = req.getLocale();
            // Continue
            next();
        }
    };
};

1 个答案:

答案 0 :(得分:4)

您可以使用case表达式

from sqlalchemy import case

@func.expression
def func(cls):
    return case(
        [
            (cls.some_column == True, cls.true_column * cls.amount),
        ],
        else_=cls.false_column * cls.amount
    )
相关问题