我有以下提到的带有可选参数的方法:
customer_id
因此,当传递customer_id
时,我想通过两个过滤条件来过滤表。而且,当未传递session.query(
DivvyDbObjects.ResourceProperty.resource_id,
DivvyDbObjects.ResourceProperty.name
).filter(
(
(DivvyDbObjects.ResourceProperty.name=='customerid') &
(DivvyDbObjects.ResourceProperty.value.isnot(None))
),
DivvyDbObjects.ResourceProperty.value==customer_id
).all()
时,我只想按第一个过滤条件过滤表。
我还尝试使用AND,OR运算符来获得所需的结果,但似乎没有任何作用
@CommandLine.Command(name="myAp",
synopsisSubcommandLabel = "", usageHelpWidth = 120,
commandListHeading = "",
customSynopsis = "\n\nArguments for MyApp operation.\n"
+ "\n"
+ "Operations:\n"
+ " create Create the stuff\n"
+ " list List the stuff\n"
+ " add Add things\n"
+ " -r --type One or both of [Type1, Typ2]\n"
+ " -a --annualCost AnnualCost\n"
+ " -n --number Number of each type thing\n"
+ " subtract Subtract things\n"
+ " -r --reasons Combination of\n"
+ " fiscal (fiscal reason)\n"
+ " operational (operational reason\n"
+ " other (other reason)\n"
+ " -h Display this help message and exit\n"
)
class PicoCliParser {
// All of my other Parameters and options here
@Option(names = { "-h", "--help" }, usageHelp = true, hidden = true)
boolean helpRequested = false;
public static void main(String[] args) throws Exception {
PicoCliParser parser = new PicoCliParser();
CommandLine cmd = new CommandLine(parser);
try {
CommandLine.ParseResult parseResult = cmd.parseArgs(args);
System.err.println("parseResults: " + parseResult.matchedArgs().toString());
if (cmd.isUsageHelpRequested()) {
cmd.usage(System.out);
}
} catch (CommandLine.ParameterException e) {
}
}
}
我是SqlAlchemy的新手,需要帮助来解决此问题。 谢谢!
答案 0 :(得分:0)
解决这个问题的最简单方法是仅在customer_id
不为空的情况下运行第二个过滤器方法:
def _customer_resources_details(customer_id=''):
try:
q = session.query(
DbObjects.ResourceProperty.resource_id,
DivvyDbObjects.ResourceProperty.name
).filter(DbObjects.ResourceProperty.name =='customerid')
if customer_id:
q = q.filter(DbObjects.ResourceProperty.value == customer_id)
return q.all()
except Exception as e:
logger.error('_customer_resources_details ' + str(e))