I'm try to get all the building structures with the hazard type "High". I have this following query:
>>> 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() argument must be a string or a number, not 'dict' So, how do I get the IDs of all the FloodHazard with hazard "high"? I understand error but when I try reference.id
it returns AttributeError: 'GeoQuerySet' object has no attribute 'id'
.
values()
returns a list of dicts. You should use the values_list()
method instead:
ids = reference.values_list('id', flat=True)
Use:
evntids=EventMember.objects.values('event_id').distinct()
output in dictionary form:
QuerySet [{'event_id': 1}, {'event_id': 2}]
Use:
evntids=EventMember.objects.values_list('event_id',flat=True).distinct()
output in int form:
QuerySet [1, 2]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.