我有模型:

from django.contrib.gis.db import models


class MyModel(models.Model):
    poly = models.PolygonField()

Django设置:
DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.mysql',
        ...

当我尝试按字段筛选MyModel时,例如:
from django.contrib.gis.geos import Point
MyModel.objects.filter(poly__contains=Point(23.23452, 45.123444))

我得到错误poly
如您所见,我使用Python 3.6.0,Django==1.10.6,PyMySQL==0.7.11,mysqlclient==1.3.10。
MySQL是5.7.16
有什么办法让它起作用吗?或者我需要提取所有AttributeError: 'WKTAdapter' object has no attribute 'translate'值,然后用纯Python处理它?
完整回溯:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/django/db/models/query.py", line 232, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/django/db/models/query.py", line 256, in __iter__
    self._fetch_all()
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/django/db/models/query.py", line 1087, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/django/db/models/query.py", line 54, in __iter__
    results = compiler.execute_sql()
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 835, in execute_sql
    cursor.execute(sql, params)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 110, in execute
    return self.cursor.execute(query, args)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/pymysql/cursors.py", line 164, in execute
    query = self.mogrify(query, args)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/pymysql/cursors.py", line 143, in mogrify
    query = query % self._escape_args(args, conn)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/pymysql/cursors.py", line 118, in _escape_args
    return tuple(conn.literal(arg) for arg in args)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/pymysql/cursors.py", line 118, in <genexpr>
    return tuple(conn.literal(arg) for arg in args)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/pymysql/connections.py", line 821, in literal
    return self.escape(obj, self.encoders)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/pymysql/connections.py", line 814, in escape
    return escape_item(obj, self.charset, mapping=mapping)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/pymysql/converters.py", line 27, in escape_item
    val = encoder(val, mapping)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/pymysql/converters.py", line 110, in escape_unicode
    return u"'%s'" % _escape_unicode(value)
  File "/Users/ailove/Work/breeders/python3/lib/python3.6/site-packages/pymysql/converters.py", line 73, in _escape_unicode
    return value.translate(_escape_table)
AttributeError: 'WKTAdapter' object has no attribute 'translate'

最佳答案

所以我进行了调试,但没有什么好消息。
gis字段的默认适配器是WKTAdapter(对于我的基础设施),它有两个属性sridwkt。在wkt中,它存储实际上需要在查询中使用的str
所以我们有需要转换为WKTAdapterstr对象,而pymysql不能这样做。
只有一个简单的选择是使用无法创建的查询并将其作为原始SQL运行。

SELECT mymodel.id  # other fields
FROM mymodel
WHERE MBRWithin(mymodel.poly,
                ST_GeomFromText('POINT (23.23452, 45.123444)'));

然后做任何你喜欢的事情,把它转换成MyModel实例或者使用你的原始值。
希望这会有帮助。

10-06 10:41