我正在尝试使用以下模型让UUIDField在python django中工作:
class Ticket(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=50)
但是,当我尝试将模型实例添加到数据库时,它会返回以下错误: / tickets /的TypeError 强制转换为Unicode:需要字符串或缓冲区,找到UUID
UUIDField的文档很简短,对我没有帮助。
编辑:我确实导入了uuid,这里是完整的回溯: 环境:
Request Method: GET
Request URL: http://127.0.0.1:8000/tickets/
Django Version: 1.8.5
Python Version: 2.7.9
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'events',
'rest_framework')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/rest_framework/viewsets.py" in view
87. return self.dispatch(request, *args, **kwargs)
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
466. response = self.handle_exception(exc)
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
463. response = handler(request, *args, **kwargs)
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/rest_framework/mixins.py" in list
48. return Response(serializer.data)
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/rest_framework/serializers.py" in data
663. ret = super(ListSerializer, self).data
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/rest_framework/serializers.py" in data
228. self._data = self.to_representation(self.instance)
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation
603. self.child.to_representation(item) for item in iterable
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/rest_framework/serializers.py" in to_representation
461. ret[field.field_name] = field.to_representation(attribute)
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/rest_framework/relations.py" in to_representation
356. name = self.get_name(value)
File "/Users/daviddejong/.virtualenvs/restDemo/lib/python2.7/site-packages/rest_framework/relations.py" in get_name
276. return six.text_type(obj)
Exception Type: TypeError at /tickets/
Exception Value: coercing to Unicode: need string or buffer, UUID found
编辑2:我的序列化器看起来像这样:
class TicketSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Ticket
fields = ('url', 'id', 'name')
编辑3:
我发现了错误。我在我的模型中犯了一个错误。我的unicode看起来像这样:
def __unicode__(self):
return self.id
当然这不起作用。
答案 0 :(得分:0)
可能你应该使用这样的东西:
def __unicode__(self):
return str(self.id)