我有一个叫django的模型 Tree
表示树对象并包含 x
及 y
类型的坐标字段 FloatField
. 我想打电话给django's update
方法来初始化 PointField
打电话 coordinates
对于每个树对象,使用以下命令:
Tree.objects.all().update(coordinates=Point(F('x'), F('y')))
如果我从文档中理解正确,我需要使用 F()
表达式来访问数据库级别上每个树对象的字段。
但是,这不起作用,并导致以下错误:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args,**cmd_options)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
output = self.handle(*args,**options)
File "/usr/src/app/app/tree_api/management/commands/update_coords.py", line 47, in handle
Tree.objects.all().update(coordinates=Point(F('x'), F('y')))
File "/usr/local/lib/python3.8/site-packages/django/contrib/gis/geos/point.py", line 35, in __init__
raise TypeError('Invalid parameters given for Point initialization.')
TypeError: Invalid parameters given for Point initialization.
我试图实现的目标(使用postgresql和postgis)可以使用以下原始sql查询完成:
UPDATE tree_column_name
SET coordinates = ST_GeomFromText('POINT(' || x || ' ' || y || ')');
有没有可能初始化 PointField
从…起 x
及 y
使用django orm的值 update
方法而不必编写原始sql查询?
1条答案
按热度按时间kmbjn2e31#
让我们试试看
Func
: