I have a really weird problem were I'm getting that a valid UUID is not a valid UUID, for example:
'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:
My models have an UUIDField as a pk, the error seems to happen randomly and won't dissapear until I restart the server, when the server restarts it works ok, so I'm a little lost here, I use django 1.10.7, postgresql 9.6.3, python 3.4.3 in amazon aws.
Edit:
Model causing trouble
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)
The model 'BaseTransaccion' also has
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Edit 2:
The code that triggers this error is the following
serial_movimientos = ReciboSerializer(recibos, many=True)
data = {
'movimientos': serial_movimientos.data, # Error happens here
}
the serializer is pretty standard (serializer methods removed)
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', )
The rest of the traceback is:
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})
A valid UUID should have 5 sections separated by a dash ( - ) and in the first section it should have 8 characters, the second, third, and the fourth section should have 4 characters each, and the last section should have 12 characters with a total of 32 characters.
UUID, Universal Unique Identifier, is a python library that helps in generating random objects of 128 bits as ids. It provides the uniqueness as it generates ids on the basis of time, Computer hardware (MAC etc.). Universally unique identifiers are a good alternative to AutoField for primary_key .
Found a related Django issue and also mod_wsgi issue. For me, adding:
WSGIApplicationGroup %{GLOBAL}
to Apache config seems to have worked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With