geoDjango - coercing to Unicode: need string or buffer, LineString found
I'm running a Django installation with geoDjango, and have created a model like this:
#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)
This worked, and I was able to create linestring objects, within terminal, and admittedly I never got them to really show, but that was more with my displaying not being fully finished yet.
But then I saw a screencast, although from 2008, with the geodjango creator - bronn, where he got an openlayer open street map inteface on the admin page, and I followed his instructions adding an admin.py with an OSMGeoAdmin ending up like this:
#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)
This is also working fine - I get a map on which I can draw a linestring, but when i try to save it - or if I have a Route object already created and try to view the Route list on adminpage - I get an error. More specifically "coercing to Unicode: need string or buffer, LineString found" and from what I understand this comes from trying to unicode a object that doesn't have a __ unicode__ function(or something like this).
So I checked the linestring object definition and there was no unicode function so I did a feeble attempt of adding one, with no luck.
So here I am, stumped. Anyone knows how I may solve this problem?
edit stacktrace:
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
No need to look for an other way, Seth is rigth!
def unicode function expects a string. So you must put a field of type Character in the 'return' part.
This should work:
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
I was able to duplicate your stack trace by the following:
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
I loaded the admin page with name defined as a CharField, then changed the model definition to be a LineStringField. When I saved the page, I got the same error.
As such, I suspect it's a data problem - does your model definition match the column definition in the database? You might try dropping that table and running manage.py syncdb again.
加载中,请稍侯......
精彩评论