Skip to content

Instantly share code, notes, and snippets.

@yuanxu
Created June 29, 2013 15:08
Show Gist options
  • Save yuanxu/5891463 to your computer and use it in GitHub Desktop.
Save yuanxu/5891463 to your computer and use it in GitHub Desktop.
一个生成django queryset.filter参数的例子。核心内容:只需按照常规的filter写法将参数和值拆分为一个tuple即可。如,qs.filter(id=5)对应为(id,5)。如需复杂的条件,需要继承自django.utils.tree.Node。
MODE_PROVINCE = 10000
MODE_CITY = 100
def D(district_field_name, district_id):
'''
获取区域选筛选表达式
@param district_field_name:
@param district_id:
@return:(字段表达式,值)
'''
assert isinstance(district_id, int)
if district_id % MODE_PROVINCE == 0: # 省级单位
return '%s__range' % district_field_name, (district_id, district_id + MODE_PROVINCE)
if district_id % MODE_CITY == 0: # 市级单位
return '%s__range' % district_field_name, (district_id, district_id + MODE_CITY)
else:
return district_field_name, district_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment