[Django]ModelChoiceFieldで動的に作成した選択肢がModel objectになるときの対処

#現象
ModelからFormに選択肢を追加したい場合、ModelChoiceFieldを用いるが、
選択肢がObjectとなってしまうときの対処法。
現象はこんな感じ。
![object_choice.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/587635/eda844ed-f3bb-8b83-3d34-573dafe6a661.png)

※ユーザープロフィールを作成するフォームに、国籍のフォームがあるイメージ。

#この時点のコード
```python:country/models.py
from django.db import models

class country(models.Model):
  name = models.CharField(max_length=100)

  class Meta:
    db_table = 'country'
```

```python:user/profile/views.py
from django.shortcuts import render
from django.views.generic import FormView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from .forms import ProfileCreateForm
from country.models import Country

class ProfileCreateView(LoginRequiredMixin, FormView):
    form_class = ProfileCreateForm
    template_name = 'user/profile/create.html'
    success_url = reverse_lazy('user:dashboard')

    def get(self, request, **kwargs):
        form = ProfileCreateForm()
        form.fields['country'].queryset = Country.objects.all()
        datas = {'form': form}
        return render(self.request, self.template_name, datas)
```

```python:user/profile/forms.py
from django import forms
from user.models import UserProfile
from country.models import Country

class ProfileCreateForm(forms.ModelForm):
    country = forms.ModelChoiceField(queryset=Country.objects.all(), empty_label='Select your Country')

    class Meta:
        model = UserProfile
        fields = ('name', 'country')
```

```python:template/user/profile/create.html
{{form.country}}
```

#対処法
ModelChoiceFieldのlabel_from_instance()をオーバーライドする。

```python:country/forms.py
from django import forms

class CustomModelChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.name
```

```python:user/profile/forms.py
from django import forms
from user.models import UserProfile
from country.models import Country
from country.forms import CustomModelChoiceField

class ProfileCreateForm(forms.ModelForm):
    country = CustomModelChoiceField(queryset=Country.objects.all(), empty_label='Select your Country')

    class Meta:
        model = UserProfile
        fields = ('name', 'country')
```

![name_choice.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/587635/8cc4a4ea-e21c-04fb-4ab7-6480f8bdd4e9.png)

#DB格納時にModel objectになる時の対応

```python:user/profile/views.py
from django.shortcuts import render
from django.views.generic import FormView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from .forms import ProfileCreateForm
from country.models import Country

class ProfileCreateView(LoginRequiredMixin, FormView):
    form_class = ProfileCreateForm
    template_name = 'user/profile/create.html'
    success_url = reverse_lazy('user:dashboard')

    def get(self, request, **kwargs):
        form = ProfileCreateForm()
        form.fields['country'].queryset = Country.objects.all()
        datas = {'form': form}
        return render(self.request, self.template_name, datas)

    def form_valid(self, form):
        profile = form.save(commit=False)
        profile.country = self.request.POST['country']  # ここ
        profile.save()
        return super().form_valid(form)
```

#参考
[ModelChoiceField(英語)](https://docs.djangoproject.com/en/3.2/ref/forms/fields/#modelchoicefield)