TypeError:int()参数必须是字符串或数字,而不是'dict'

时间:2015-02-27 07:57:04

标签: python django geodjango

我试图让所有建筑结构的危险类型为“高”。我有以下查询:

>>> reference = FloodHazard.objects.filter(hazard='High')
>>> ids = reference.values('id')
>>> for id in ids:
...     getgeom = FloodHazard.objects.get(id=id).geom
...     getbldg = BuildingStructure.objects.filter(geom__intersects=getgeom).value_list('id')

Traceback (most recent call last):
  File "<console>", line 3, in <module>
  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 151, in get
    return self.get_queryset().get(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 298, in get
    clone = self.filter(*args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 590, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 608, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1198, in add_q
    clause = self._add_q(where_part, used_aliases)
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1234, in _add_q
    current_negated=current_negated)
  File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1125, in build_filter
    clause.add(constraint, AND)
  File "C:\Python27\lib\site-packages\django\utils\tree.py", line 104, in add
    data = self._prepare_data(data)
  File "C:\Python27\lib\site-packages\django\contrib\gis\db\models\sql\where.py", line 42, in _prepare_data
    return super(GeoWhereNode, self)._prepare_data(data)
  File "C:\Python27\lib\site-packages\django\db\models\sql\where.py", line 79, in _prepare_data
    value = obj.prepare(lookup_type, value)
  File "C:\Python27\lib\site-packages\django\db\models\sql\where.py", line 352, in prepare
    return self.field.get_prep_lookup(lookup_type, value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 369, in get_prep_lookup
    return self.get_prep_value(value)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 613, in get_prep_value
    return int(value)

TypeError:int()参数必须是字符串或数字,而不是'dict' 那么,我如何获得具有“高”危险的所有FloodHazard的ID?我理解错误,但当我尝试reference.id时,它会返回AttributeError: 'GeoQuerySet' object has no attribute 'id'

2 个答案:

答案 0 :(得分:4)

values()返回一个dicts列表。您应该使用values_list()方法:

ids = reference.values_list('id', flat=True)

答案 1 :(得分:1)

使用:

evntids=EventMember.objects.values('event_id').distinct()

以字典形式输出:

QuerySet [{'event_id': 1}, {'event_id': 2}]

使用:

 evntids=EventMember.objects.values_list('event_id',flat=True).distinct()

以int形式输出:

QuerySet [1, 2]