【发布时间】:2018-02-09 22:43:33
【问题描述】:
我遇到了一个非常奇怪的问题,即有效的 UUID 不是有效的 UUID,例如:
'fd31b6b5-325d-4b65-b496-d7e4d16c8a93' is not a valid UUID.
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value
2371. value = uuid.UUID(value)
File "/usr/lib64/python3.4/uuid.py" in __init__
134. hex = hex.replace('urn:', '').replace('uuid:', '')
During handling of the above exception ('UUID' object has no attribute 'replace'), another exception occurred:
我的模型有一个 UUIDField 作为 pk,错误似乎是随机发生的,直到我重新启动服务器才会消失,当服务器重新启动时它工作正常,所以我在这里有点迷失,我使用 django 1.10 .7、postgresql 9.6.3、亚马逊 AWS 中的 python 3.4.3。
编辑:
导致问题的模型
class ReciboTransaccion(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
ingreso = models.ForeignKey('banco.BaseTransaccion', related_name='recibo_ingreso',
on_delete=models.PROTECT,
null=True, blank=True,
editable=False)
egreso = models.OneToOneField('banco.BaseTransaccion', related_name='recibo_egreso',
on_delete=models.PROTECT,
null=True, blank=True,
editable=False)
fecha = models.DateTimeField(auto_now_add=True)
模型'BaseTransaccion'也有
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
编辑 2:
触发此错误的代码如下
serial_movimientos = ReciboSerializer(recibos, many=True)
data = {
'movimientos': serial_movimientos.data, # Error happens here
}
序列化器非常标准(序列化器方法被移除)
class ReciboSerializer(serializers.ModelSerializer):
ingreso = serializers.SerializerMethodField()
egresos = serializers.SerializerMethodField()
descripcion = serializers.SerializerMethodField()
monedero_generado = serializers.SerializerMethodField()
reembolsada = serializers.SerializerMethodField()
class Meta:
model = ReciboTransaccion
fields = ('ingreso', 'egresos', 'descripcion', 'monedero_generado', 'reembolsada', 'fecha', )
其余的回溯是:
File "/opt/python/run/venv/lib/python3.4/site-packages/django/core/handlers/exception.py" in inner
42. response = get_response(request)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/core/handlers/base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/opt/python/current/app/myproject/main/ws.py" in index
129. json_data = func(request)
File "/opt/python/current/app/myproject/main/ws.py" in get_datos_home
534. 'movimientos': serial_movimientos.data,
File "/opt/python/run/venv/lib/python3.4/site-packages/rest_framework/serializers.py" in data
739. ret = super(ListSerializer, self).data
File "/opt/python/run/venv/lib/python3.4/site-packages/rest_framework/serializers.py" in data
263. self._data = self.to_representation(self.instance)
File "/opt/python/run/venv/lib/python3.4/site-packages/rest_framework/serializers.py" in to_representation
657. self.child.to_representation(item) for item in iterable
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/query.py" in __iter__
256. self._fetch_all()
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/query.py" in _fetch_all
1087. self._result_cache = list(self.iterator())
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/query.py" in __iter__
54. results = compiler.execute_sql()
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql
824. sql, params = self.as_sql()
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in as_sql
376. where, w_params = self.compile(self.where) if self.where is not None else ("", [])
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in compile
353. sql, params = node.as_sql(self, self.connection)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/sql/where.py" in as_sql
79. sql, params = compiler.compile(child)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in compile
353. sql, params = node.as_sql(self, self.connection)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/lookups.py" in as_sql
297. return super(In, self).as_sql(compiler, connection)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/lookups.py" in as_sql
156. rhs_sql, rhs_params = self.process_rhs(compiler, connection)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/lookups.py" in process_rhs
284. sqls, sqls_params = self.batch_process_rhs(compiler, connection, rhs)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/lookups.py" in batch_process_rhs
51. _, params = self.get_db_prep_lookup(rhs, connection)
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/lookups.py" in get_db_prep_lookup
181. if self.get_db_prep_lookup_value_is_iterable else
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/lookups.py" in <listcomp>
180. [get_db_prep_value(v, connection, prepared=True) for v in value]
File "/opt/python/run/venv/lib/python3.4/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value
2373. raise TypeError(self.error_messages['invalid'] % {'value': value})
【问题讨论】:
-
你能告诉我们你的模型吗?
UUIDField是主键吗? -
当您执行
uuid.UUID(uuid.UUID('fd31b6b5-325d-4b65-b496-d7e4d16c8a93'))之类的操作时会发生这种情况。uuid.UUID()不支持使用已经是 UUID 的参数调用它的情况。 Django 捕获了that case,所以有些可疑。您可以在字段转换之前添加一些记录value的代码吗? -
它发生在一个 django rest 框架序列化程序中,让我很困惑的是完全相同的代码,如果我重新启动服务器并且一段时间后它会再次发生并且不会消失,那么场景就可以工作直到我重新开始
-
我的问题与 Jorge Alfaro 完全相同,重新启动服务器可以暂时解决问题。问题开始随机发生,我的猜测是图书馆可能在某个时候更新了自己。我在 python 2.7.12、AWS RDS PSQL 9.5.2、Django 1.11.4
-
为什么序列化器中的所有字段都是SerializerMethodField类型的?使用UUIDField 不是更有意义吗?至少在纯 UUID 上
标签: python django postgresql amazon-web-services django-rest-framework