If you want to use choicefield from Model
object, you can use ModelChoiceField
.
user = forms.ModelChoiceField(queryset=User.objects.filter(is_active=True))
Or you can use ChoiceField
with __init__
for change display.
class UserForm(forms.Form):
user = forms.ChoiceField(choices = [])
def __init__(self, *args, **kwargs):
super(UserForm, self).__init__(*args, **kwargs)
self.fields[user].choices = [(u.phone, u.name) for u in User.objects.all()]
'backend > django' 카테고리의 다른 글
Django: custom filter for list_filter in admin (0) | 2018.05.05 |
---|---|
Django: check changed fields in Model save() method (0) | 2018.05.04 |
Django: disable password check (password2) in django-allauth (0) | 2018.05.02 |
Django: reset migrations (0) | 2018.04.30 |
Django: custom list_filter in ModelAdmin (0) | 2018.04.29 |