本文整理汇总了Python中django.db.models.sql.query.Query.names_to_path方法的典型用法代码示例。如果您正苦于以下问题:Python Query.names_to_path方法的具体用法?Python Query.names_to_path怎么用?Python Query.names_to_path使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.models.sql.query.Query
的用法示例。
在下文中一共展示了Query.names_to_path方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_field
# 需要导入模块: from django.db.models.sql.query import Query [as 别名]
# 或者: from django.db.models.sql.query.Query import names_to_path [as 别名]
def _get_field(model, name):
# Create a fake query object so we can easily work out what field
# type we are dealing with
qs = Query(model)
parts = name.split(LOOKUP_SEP)
# The following is borrowed from the innards of Query.add_filter - it strips out __gt, __exact et al.
num_parts = len(parts)
if num_parts > 1 and parts[-1] in QUERY_TERMS:
# Traverse the lookup query to distinguish related fields from
# lookup types.
for counter, field_name in enumerate(parts, 1):
try:
lookup_field = model._meta.get_field(field_name)
except FieldDoesNotExist:
# Not a field. Bail out.
parts.pop()
break
# Unless we're at the end of the list of lookups, let's attempt
# to continue traversing relations.
if counter < num_parts:
try:
model = lookup_field.rel.to
except AttributeError:
# Not a related field. Bail out.
parts.pop()
break
return qs.names_to_path(parts, qs.get_meta(), True, fail_on_missing=False)[1]