# django.core.exceptions.ValidationError: {'model': [ValidationError(['“test” is not a valid UUID.'])]}

- **ID:** `python/django-model-foreignkey-validation`
- **Domain:** python
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

外键字段赋值了非 UUID 格式的字符串，但模型期望 UUID 类型

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.2 | active | — | — |
| 4.0 | active | — | — |

## Workarounds

1. **传入有效的 UUID 字符串** (100% success)
   ```
   import uuid; model_instance.foreign_key = uuid.UUID('550e8400-e29b-41d4-a716-446655440000')
   ```
2. **使用 UUID 对象而不是字符串** (95% success)
   ```
   from uuid import UUID; model_instance.foreign_key = UUID('550e8400-e29b-41d4-a716-446655440000')
   ```

## Dead Ends

- **忽略验证直接保存** — Django 在保存前会进行字段验证，无法绕过 (100% fail)
- **将字段类型改为 CharField** — 改变模型结构需要迁移，且可能破坏外键关系 (60% fail)
