geoDjango - 强制转换为Unicode:需要字符串或缓冲区,找到LineString

时间:2010-07-16 18:57:07

标签: django geodjango

我正在使用geoDjango运行Django安装,并创建了这样的模型:

#models.py
from django.contrib.gis.db import models

class Route(models.Model):
    name = models.CharField(max_length=100)

    path = models.LineStringField(srid=4326)
    objects = models.GeoManager()

    def __unicode__(self):
        return str(self.path)

这很有用,我能够在终端内创建线串对象,不可否认,我从来没有真正展示它们,但更多的是我的显示尚未完全显示。

但后来我看到了一个截屏视频,尽管从2008年开始,还有geodjango创作者 - bronn,在管理页面上有一个开放的街道地图界面,我按照他的指示添加了一个带有OSMGeoAdmin的admin.py像这样:

#admin.py
from django.contrib.gis import admin
from myproject.georoute.models import Route


class RouteAdmin(admin.OSMGeoAdmin):
    search_fields = ('name',)

admin.site.register(Route, RouteAdmin)

这也工作得很好 - 我得到了一个我可以绘制线串的地图,但是当我尝试保存它时 - 或者如果我已经创建了一个Route对象并尝试在管理页面上查看路由列表 - 我得到一个错误。更具体地说,“强制转换为Unicode:需要字符串或缓冲区,找到LineString”,据我所知,这来自于尝试unicode一个没有__ unicode__函数的对象(或类似的东西)

所以我检查了线串对象的定义,并且没有unicode函数,所以我做了一个微弱的尝试,添加一个,没有运气。

所以我在这里,难倒。谁知道我怎么解决这个问题?

编辑 堆栈跟踪:

Environment:

Request Method: POST
Request URL: http://storm.webfactional.com/admin/georoute/route/add/
Django Version: 1.2.1
Python Version: 2.6.5
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.gis',
 'django.contrib.admin',
 'registration',
 'georoute']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/home/storm/webapps/django/lib/python2.6/django/core/handlers/base.py" in get_response
  100.                     response = callback(request, *callback_args, **callback_kwargs)
File "/home/storm/webapps/django/lib/python2.6/django/contrib/admin/options.py" in wrapper
  239.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/storm/webapps/django/lib/python2.6/django/utils/decorators.py" in _wrapped_view
  76.                     response = view_func(request, *args, **kwargs)
File "/home/storm/webapps/django/lib/python2.6/django/views/decorators/cache.py" in _wrapped_view_func
  69.         response = view_func(request, *args, **kwargs)
File "/home/storm/webapps/django/lib/python2.6/django/contrib/admin/sites.py" in inner
  190.             return view(request, *args, **kwargs)
File "/home/storm/webapps/django/lib/python2.6/django/utils/decorators.py" in _wrapper
  21.             return decorator(bound_func)(*args, **kwargs)
File "/home/storm/webapps/django/lib/python2.6/django/utils/decorators.py" in _wrapped_view
  76.                     response = view_func(request, *args, **kwargs)
File "/home/storm/webapps/django/lib/python2.6/django/utils/decorators.py" in bound_func
  17.                 return func(self, *args2, **kwargs2)
File "/home/storm/webapps/django/lib/python2.6/django/db/transaction.py" in _commit_on_success
  299.                     res = func(*args, **kw)
File "/home/storm/webapps/django/lib/python2.6/django/contrib/admin/options.py" in add_view
  800.                 self.log_addition(request, new_object)
File "/home/storm/webapps/django/lib/python2.6/django/contrib/admin/options.py" in log_addition
  428.             object_repr     = force_unicode(object),
File "/home/storm/webapps/django/lib/python2.6/django/utils/encoding.py" in force_unicode
  66.                 s = unicode(s)

Exception Type: TypeError at /admin/georoute/route/add/
Exception Value: coercing to Unicode: need string or buffer, LineString found

2 个答案:

答案 0 :(得分:1)

无需寻找其他方式,Seth非常严格!

def unicode 函数需要一个字符串。所以你必须在'return'部分放置一个Character类型的字段。

这应该有效:

from django.contrib.gis.db import Models
# model from the tutorial
class WorldBorders(models.Model):
    name = models.CharField(max_length=50)
    path = models.LineStringField(srid=4326)
    ...
    def __unicode__(self):
        return self.name

答案 1 :(得分:0)

我能够通过以下方式复制堆栈跟踪:

from django.contrib.gis.db import Models
# model from the tutorial
class WorldBorders(models.Model):
    #name = models.CharField(max_length=50)
    name = models.LineStringField(srid=4326)
    ...
    def __unicode__(self):
        return self.name

我加载了管理页面,name定义为CharField,然后将模型定义更改为LineStringField。当我保存页面时,我得到了同样的错误。

因此,我怀疑这是一个数据问题 - 您的模型定义是否与数据库中的列定义匹配?您可以尝试删除该表并再次运行manage.py syncdb