## 事象
[[Django]]4.2と[[Django REST framework]]、[[django-stubs]]を使っているが、取得したmodelsの[[属性 (Python)|属性]]が`Unknown` とみなされ推論できない。期待値は `str`。
`models.py`
```python
# 中略
class Animal(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=100, blank=True, default="")
# 中略
```
```python
from project.app.models import Animal
animal = Animal.objects.first()
if animal:
animal.name
# ^ ^
# Animal Unknown
```
### 環境
```
├── django v4.2
│ ├── asgiref v3.11.0
│ └── sqlparse v0.5.5
├── django-stubs v5.2.8
│ ├── django v4.2 (*)
│ ├── django-stubs-ext v5.2.8
│ │ ├── django v4.2 (*)
│ │ └── typing-extensions v4.15.0
│ ├── types-pyyaml v6.0.12.20250915
│ └── typing-extensions v4.15.0
├── djangorestframework v3.16.1
│ └── django v4.2 (*)
└── ruff v0.14.11
```
## 原因
不明。(対応していないから?)
## 解決方法
[[django-types]]を使う。
```console
uv add --dev django-types
```
解決する。
```python
from project.app.models import Animal
animal = Animal.objects.first()
if animal:
animal.name
# ^ ^
# Animal str
```
> [!caution]
> なお、[[django-stubs]]はなくても期待通り動く。そもそも[[django-stubs]]は[[VSCode]]([[Pylance]])や[[Neovim]]([[Pyright]])では ([[mypy]]未対応のため) 動かないから使わないほうがいいかも?
>
> [[📰Django REST frameworkとPyright with Neovimで型安全を実現するための調査]] も参照。