我对Python还很陌生,正在尝试找到一种更好的方法来编写代码。一定有办法,只是不知道怎么做
这两个查询本质上是相同的,因此必须有一种方法来减少。有没有更有效的方法
if set_date is not None:
if is_rejected != 'true':
query = query\
.filter(createddate__lte=set_date) \
.car_statuses(CarTow.TOWED) \
.order_by('-createddate')
else:
query = query\
.filter(createddate__lte=set_date) \
.car_statuses(CarTow.TOWED,CarTow.CONFIRMED) \
.order_by('-createddate')
return query
抱歉,如果这是个简单的问题,新手
您可以通过将不同的参数拉入if语句来简化;把普通的东西放在外面
您可能要替换此:
有了这个:
看看Python是如何计算if条件的: Truth Value Testing (pydocs)
另外,“is”会给出一些奇怪的结果,它实际上是用来确定两个标签是否引用同一个对象的。 Understanding Python's is operator
您可以使用三元逻辑来添加元组
相关问题 更多 >
编程相关推荐