从数据库值填充Django ChoiceField [英] Django ChoiceField populated from database values

查看:357
本文介绍了从数据库值填充Django ChoiceField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ChoiceField在数据库中创建值下拉列表时遇到问题。以下是代码片段

I am having problems using a ChoiceField to create a drop down list of values in the database. Here is the snippet of code

from django import forms
from testplatform.models import ServiceOffering

class ContactForm(forms.Form):

    subject = forms.ChoiceField(queryset=ServiceOffering.objects.all())
    #subject = forms.ModelMultipleChoiceField(queryset=ServiceOffering.objects.all())

#subject ....行工作,但是当我使用行ChoiceField(queryset ....)我得到以下错误。

The #subject.... line works, but when I use the line ChoiceField(queryset....) I get the following error.

__init__() got an unexpected keyword argument 'queryset'

任何想法?

推荐答案

ChoiceField 没有查询器。您正在寻找 ModelChoiceField

ChoiceField doesn't have a queryset. You're looking for ModelChoiceField

这篇关于从数据库值填充Django ChoiceField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆