我要获取x
和 y
从以下点开始的坐标 queryset
:
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
,而且我还没有找到以最佳方式对其进行序列化的方法。
最佳答案
您可以访问 __dict__
对象的方法,然后直接使用几何图形:
user = User.objects.values('id', 'email', 'username', 'point').first()
user_dict = user.__dict__
user_dict.update({'point__x':user_dict['point'].x, {'point__y':user_dict['point'].y})
关于python - Django获取PointField的坐标值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54137877/