# TypeError: 类型为'MyModel'的对象不是JSON可序列化的

- **ID:** `python/django-json-response-serialization`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

尝试在JsonResponse中直接返回Django模型实例，而未序列化为字典。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Use model_to_dict: from django.forms.models import model_to_dict; return JsonResponse(model_to_dict(instance))
   ```
2. **** (90% 成功率)
   ```
   Create a custom serializer: return JsonResponse({'id': instance.id, 'name': instance.name})
   ```

## 无效尝试

- **** — Produces a string representation, not useful JSON data. (80% 失败率)
- **** — Still fails because model instance is not JSON serializable. (90% 失败率)
