[Django] ModelChoiceField에서 동적으로 만든 옵션이 Model object가 될 때의 조치
12660 단어 장고
현상
Model에서 Form으로 선택 사항을 추가하려면 ModelChoiceField를 사용하지만,
선택사항이 Object가 되어 버릴 때의 대처법.
현상은 이런 느낌.
※유저 프로필을 작성하는 폼에, 국적의 폼이 있는 이미지.
이 시점의 코드
country/models.pyfrom django.db import models
class country(models.Model):
name = models.CharField(max_length=100)
class Meta:
db_table = 'country'
user/profile/views.pyfrom 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)
user/profile/forms.pyfrom 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')
template/user/profile/create.html{{form.country}}
대처법
ModelChoiceField의 label_from_instance()를 재정의합니다.
country/forms.pyfrom django import forms
class CustomModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return obj.name
user/profile/forms.pyfrom 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')
DB 저장시 Model 객체가 될 때의 대응
user/profile/views.pyfrom 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(영어)
Reference
이 문제에 관하여([Django] ModelChoiceField에서 동적으로 만든 옵션이 Model object가 될 때의 조치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/10th_ten/items/45951b44ba7923737a9f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
country/models.py
from django.db import models
class country(models.Model):
name = models.CharField(max_length=100)
class Meta:
db_table = 'country'
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)
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')
template/user/profile/create.html
{{form.country}}
대처법
ModelChoiceField의 label_from_instance()를 재정의합니다.
country/forms.pyfrom django import forms
class CustomModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return obj.name
user/profile/forms.pyfrom 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')
DB 저장시 Model 객체가 될 때의 대응
user/profile/views.pyfrom 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(영어)
Reference
이 문제에 관하여([Django] ModelChoiceField에서 동적으로 만든 옵션이 Model object가 될 때의 조치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/10th_ten/items/45951b44ba7923737a9f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
from django import forms
class CustomModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return obj.name
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')
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(영어)
Reference
이 문제에 관하여([Django] ModelChoiceField에서 동적으로 만든 옵션이 Model object가 될 때의 조치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/10th_ten/items/45951b44ba7923737a9f
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여([Django] ModelChoiceField에서 동적으로 만든 옵션이 Model object가 될 때의 조치), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/10th_ten/items/45951b44ba7923737a9f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)