【发布时间】:2017-11-17 16:56:49
【问题描述】:
我有一个使用纬度和经度字段进行定位的模型。我想使用查询参数运行的查询之一是围绕特定半径进行搜索。我已读取查询集并覆盖它:
queryset = super().get_queryset(request)
if 'radius' in request.GET:
queryset = queryset.in_distance(request.GET['radius'], fields=['location__latitude',location__longitude'], points=[lat, lon])
return queryset
当使用/admin/dal/listing?radius=50 调用我的管理页面时,我会被重定向到没有查询字符串的管理页面。
按照 Django 的代码,发现了这个:
# At this point, all the parameters used by the various ListFilters
# have been removed from lookup_params, which now only contains other
# parameters passed via the query string. We now loop through the
# remaining parameters both to ensure that all the parameters are valid
# fields and to determine if at least one of them needs distinct(). If
# the lookup parameters aren't real fields, then bail out.
try:
for key, value in lookup_params.items():
lookup_params[key] = prepare_lookup_value(key, value)
use_distinct = use_distinct or lookup_needs_distinct(self.lookup_opts, key)
return filter_specs, bool(filter_specs), lookup_params, use_distinct
except FieldDoesNotExist as e:
six.reraise(IncorrectLookupParameters, IncorrectLookupParameters(e), sys.exc_info()[2])
简而言之,因为查询字符串不是已知字段,django 会优雅地恐慌并重定向一个未过滤的管理页面。
我能做什么?
【问题讨论】:
标签: django django-admin