GeoDjango:ValueError,无法将字符串转换为浮点数
GeoDjango: ValueError, could not convert string to float
昨天我在 简单模型上遇到了同样的问题,我已经设法解决了这个问题。
今天我修改了以下模型:
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
from mapbox_location_field.models import LocationField
class IllegalDumpCategory(models.Model):
category = models.CharField(
max_length=50,
)
slug = models.SlugField(
max_length=50,
unique=True,
)
description = models.TextField(
max_length=500,
)
class IllegalDumpGeo(models.Model):
user_name = models.CharField(
'Username',
max_length=50,
)
user_name_email = models.EmailField(
'Email',
max_length=254,
)
description = models.TextField(
max_length=500,
)
image_1 = models.ImageField(
upload_to='%Y/%m/%d',
blank=False,
null=False,
)
image_2 = models.ImageField(
upload_to='%Y/%m/%d',
blank=True,
null=True,
)
image_3 = models.ImageField(
upload_to='%Y/%m/%d',
blank=True,
null=True,
)
volume_extension = models.PositiveIntegerField(
blank=False,
null=False,
)
link = models.URLField(
blank=True,
null=True,
)
waste_type = models.ForeignKey(
IllegalDumpCategory,
on_delete=models.CASCADE,
)
geom = models.PointField(
blank=True,
null=True,
)
location = LocationField()
def __int__(self):
return self.pk
def save(self, *args, **kwargs):
lat = self.location[0]
lon = self.location[1]
self.geom = Point(x=lon, y=lat, srid=4326)
super(IllegalDumpGeo, self).save(*args, **kwargs)
@property
def coordinates(self):
return str(self.geom.x) + ', ' + str(self.geom.y)
我正尝试通过管理面板添加一些点,但我再次看到此错误:
ValueError could not convert string to float:
'6.01245266838146,-10.16992187499897'
为什么会这样?
追溯here
看起来有点奇怪,但查看 Traceback 我想我发现了问题:
File "/home/max/Django/gealogos/devenv/lib/python3.6/site-packages/mapbox_location_field/widgets.py"
in parse_tuple_string
6. return tuple(map(float, tuple_string[1:-1].split(", ")))
tuple(map(float, tuple_string[1:-1].split(", ")))
方法试图通过 ', '
(一个逗号后跟 a space)来分割输入,但是你传递的是以下 lan_lon: '6.01245266838146,-10.16992187499897'
逗号后没有 space 因此 split
无法按预期运行,导致尝试转换完整的字符串 '6.01245266838146,-10.16992187499897'
作为 float()
.
您需要找到并修复创建 lat_lon 字符串的代码部分(无法在您的问题中找到它,因此,我无能为力。它可能是 widget
也许吧?)在坐标之间的逗号后包含 space:
'lon_coord,<space>lat_coord'
应为您的案例生成此字符串:
'6.01245266838146, -10.16992187499897'
该错误实际上是 django-mapbox-location-field 中的错误
编辑此文件的第 6 行:
mapbox_location_field/widgets.py
并将其更改为:
def parse_tuple_string(tuple_string):
return tuple(map(float, tuple_string[1:-1].split(",")))
注意,我在拆分方法中删除了逗号后的 space。
这就解决了。
我想我应该发送 PR
昨天我在
今天我修改了以下模型:
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
from mapbox_location_field.models import LocationField
class IllegalDumpCategory(models.Model):
category = models.CharField(
max_length=50,
)
slug = models.SlugField(
max_length=50,
unique=True,
)
description = models.TextField(
max_length=500,
)
class IllegalDumpGeo(models.Model):
user_name = models.CharField(
'Username',
max_length=50,
)
user_name_email = models.EmailField(
'Email',
max_length=254,
)
description = models.TextField(
max_length=500,
)
image_1 = models.ImageField(
upload_to='%Y/%m/%d',
blank=False,
null=False,
)
image_2 = models.ImageField(
upload_to='%Y/%m/%d',
blank=True,
null=True,
)
image_3 = models.ImageField(
upload_to='%Y/%m/%d',
blank=True,
null=True,
)
volume_extension = models.PositiveIntegerField(
blank=False,
null=False,
)
link = models.URLField(
blank=True,
null=True,
)
waste_type = models.ForeignKey(
IllegalDumpCategory,
on_delete=models.CASCADE,
)
geom = models.PointField(
blank=True,
null=True,
)
location = LocationField()
def __int__(self):
return self.pk
def save(self, *args, **kwargs):
lat = self.location[0]
lon = self.location[1]
self.geom = Point(x=lon, y=lat, srid=4326)
super(IllegalDumpGeo, self).save(*args, **kwargs)
@property
def coordinates(self):
return str(self.geom.x) + ', ' + str(self.geom.y)
我正尝试通过管理面板添加一些点,但我再次看到此错误:
ValueError could not convert string to float: '6.01245266838146,-10.16992187499897'
为什么会这样?
追溯here
看起来有点奇怪,但查看 Traceback 我想我发现了问题:
File "/home/max/Django/gealogos/devenv/lib/python3.6/site-packages/mapbox_location_field/widgets.py" in parse_tuple_string 6. return tuple(map(float, tuple_string[1:-1].split(", ")))
tuple(map(float, tuple_string[1:-1].split(", ")))
方法试图通过 ', '
(一个逗号后跟 a space)来分割输入,但是你传递的是以下 lan_lon: '6.01245266838146,-10.16992187499897'
逗号后没有 space 因此 split
无法按预期运行,导致尝试转换完整的字符串 '6.01245266838146,-10.16992187499897'
作为 float()
.
您需要找到并修复创建 lat_lon 字符串的代码部分(无法在您的问题中找到它,因此,我无能为力。它可能是 widget
也许吧?)在坐标之间的逗号后包含 space:
'lon_coord,<space>lat_coord'
应为您的案例生成此字符串:
'6.01245266838146, -10.16992187499897'
该错误实际上是 django-mapbox-location-field 中的错误
编辑此文件的第 6 行: mapbox_location_field/widgets.py 并将其更改为:
def parse_tuple_string(tuple_string):
return tuple(map(float, tuple_string[1:-1].split(",")))
注意,我在拆分方法中删除了逗号后的 space。
这就解决了。 我想我应该发送 PR