Django Admin中的PointField使用GeoDjango(DjangoUnicodeDecodeError)

时间:2014-06-25 14:29:44

标签: mysql django mariadb geodjango

我正在尝试使用GeoDjango包中的PointField,每当我尝试查看具有保存点的模型对象时,我都会遇到以下异常:

Environment:


Request Method: GET
Request URL: http://localhost:1989/admin/world/worldborder/6/

Django Version: 1.6.2
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.gis',
 'world')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper
  432.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
  198.             return view(request, *args, **kwargs)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  99.                     response = view_func(request, *args, **kwargs)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/utils/decorators.py" in bound_func
  25.                 return func(self, *args2, **kwargs2)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/db/transaction.py" in inner
  339.                 return func(*args, **kwargs)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/contrib/admin/options.py" in change_view
  1237.             form = ModelForm(instance=obj)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/forms/models.py" in __init__
  315.             object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/forms/models.py" in model_to_dict
  143.             data[f.name] = f.value_from_object(instance)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in value_from_object
  560.         return getattr(obj, self.attname)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/contrib/gis/db/models/proxy.py" in __get__
  40.             geom = self._klass(geom_value)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/contrib/gis/geos/geometry.py" in __init__
  65.             geo_input = force_text(geo_input)
File "/home/mark/.virtualenvs/fanmode/local/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
  116.             raise DjangoUnicodeDecodeError(s, *e.args)

Exception Type: DjangoUnicodeDecodeError at /admin/world/worldborder/6/
Exception Value: 'utf8' codec can't decode byte 0xc0 in position 16: invalid start byte. You passed in '\x00\x00\x00\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00,0\xc08\x00\x00\x00p\xdf\x19@' (<type 'str'>)

我的模型看起来像这样:

from django.contrib.gis.db import models

# Create your models here.
class WorldBorder(models.Model):
    # Regular Django fields corresponding to the attributes in the
    # world borders shapefile.
    name = models.CharField(max_length=50)
    location = models.PointField(null=False, blank=False, srid=4326, verbose_name="Location")


    # Returns the string representation of the model.
    # On Python 3: def __str__(self):
    def __unicode__(self):
        return self.name

我正在使用MariaDB 5.5.38。 PointField的表设置为MyISAM,我尝试过编码latin1和UTF-8。

任何可能导致此问题的想法?

谢谢, 标记

1 个答案:

答案 0 :(得分:2)

答案是我忘了在模型中添加这条关键线:

objects = models.GeoManager()