diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index f4f3170..c6cf279 100644
a
|
b
|
class UUIDField(Field):
|
2371 | 2371 | if value is None: |
2372 | 2372 | return None |
2373 | 2373 | if not isinstance(value, uuid.UUID): |
2374 | | try: |
2375 | | value = uuid.UUID(value) |
2376 | | except AttributeError: |
2377 | | raise TypeError(self.error_messages['invalid'] % {'value': value}) |
| 2374 | value = self.to_python(value) |
2378 | 2375 | |
2379 | 2376 | if connection.features.has_native_uuid_field: |
2380 | 2377 | return value |
| 2378 | |
2381 | 2379 | return value.hex |
2382 | 2380 | |
2383 | 2381 | def to_python(self, value): |
2384 | | if value and not isinstance(value, uuid.UUID): |
| 2382 | if not isinstance(value, uuid.UUID): |
2385 | 2383 | try: |
2386 | 2384 | return uuid.UUID(value) |
2387 | | except ValueError: |
| 2385 | except (AttributeError, ValueError): |
2388 | 2386 | raise exceptions.ValidationError( |
2389 | 2387 | self.error_messages['invalid'], |
2390 | 2388 | code='invalid', |