【发布时间】:2019-06-05 20:46:21
【问题描述】:
我想从以下queryset 中的点获取x 和y 坐标:
user = User.objects.values('id', 'email', 'username', 'point').first()
这些是我的models:
from django.db.models import Model
from django.contrib.gis.db.models import PointField
class BasePointModel(Model):
point = PointField(null=True, spatial_index=True, geography=True)
class Meta:
abstract = True
class User(AbstractUser, BasePointModel, HashableModel): # type: ignore
# First Name and Last Name do not cover name patterns
# around the globe.
name = CharField(_("Name of User"), blank=True, max_length=255)
is_online = BooleanField(_("Is online"), default=False)
我得到以下结果:
{'id': 85270,
'email': 'username_0@email.com',
'username': 'username_0',
'point': <Point object at 0x7f8e061cc2b8>}
如何获得x 和y,使用values 进行查询集?
我想得到这样的东西:
{'id': 85270,
'email': 'username_0@email.com',
'username': 'username_0',
'point__x': '-4.266398314110177',
'point__y': '-39.39432682357033'}
我发出从数据库返回 50,000 行的请求,我注意到由于数据序列化,性能变得越来越差。我想通过返回values来最小化序列化。
但是PointField 总是返回一个object,而且我还没有找到以最佳方式对其进行序列化的方法。
【问题讨论】:
-
显示
User的模型/字段和Point的类。 -
@wim,我更新了我的问题。
-
嘿@NarnikGamarnik 我正在通过我的答案,你觉得这里的任何答案有帮助吗?
标签: python django gis postgis geodjango