有效的UUID不是一个有效的UUID。

8

我遇到了一个非常奇怪的问题,即使我输入了有效的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作为主键,在某些情况下会出现错误,直到我重新启动服务器才会消失。当服务器重新启动时,它就能正常工作,所以我有点困惑。我使用的是django 1.10.7、postgresql 9.6.3和python 3.4.3在amazon aws上。 编辑: 导致问题的模型
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)

模型'BaseTransaction'还具有
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})

2
你能展示一下你的模型吗?UUIDField 是主键吗? - Zach Gates
2
当您执行类似于uuid.UUID(uuid.UUID('fd31b6b5-325d-4b65-b496-d7e4d16c8a93'))的操作时,就会发生这种情况。uuid.UUID()不支持在调用已经是UUID的参数的情况。Django捕获了该情况,因此有些可疑。您能否添加一些代码,在字段转换之前记录value的值? - dhke
1
这是在Django Rest Framework序列化器中发生的,让我困惑的是,完全相同的代码和场景在重新启动服务器后可以正常工作,但过一段时间后又会出现问题,只有再次重启才能解决。 - Jorge Alfaro
1
我遇到了与Jorge Alfaro完全相同的问题,重新启动服务器可以暂时解决问题。而且问题开始随机出现,我的猜测是可能某个库在某个时刻更新了自己。 我使用的是Python 2.7.12,AWS RDS PSQL 9.5.2,Django 1.11.4。 - Andrés Torres Marroquín
@JorgeAlfaro 看起来问题已经解决了,你那边也好了吗?顺便说一下,我是在使用 AWS,具体来说是 Elastic Beanstalk。 - Andrés Torres Marroquín
显示剩余9条评论
2个回答

4

发现了一个相关的Django问题,还有一个mod_wsgi问题。 对于我来说,添加:

WSGIApplicationGroup %{GLOBAL}

看起来对Apache配置进行的更改已经生效。


这个对你真的有用吗?我也遇到了UUID的同样问题。 - Jinto Antony
@JintoAntony,是的,它起作用了。检查我链接的两个线程,也许有更多的提示,但基本上只有这一行对我有用。 - Ivan

1
请确保您阅读了@Ivan的答案,其中他确定了描述此问题的特定Django问题。
对于以后任何人(比如我一两年后),如果您在uwsgi中遇到相同的问题,则解决方案是修改Django项目中的uwsgi.ini文件,将以下内容包含在内:
single-interpreter=true

例如,您的整个uwsgi.ini文件可能如下所示:
[uwsgi]
chdir=/opt/theapp/
module=yourdjangoproject.wsgi:application
mount=/secure/rest=yourdjangoproject.wsgi:application
home=/opt/theapp/venv3.8
processes=10
socket = 127.0.0.1:8000
single-interpreter=true

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接