从数据库值填充Django ChoiceField

编程入门 行业动态 更新时间:2024-10-24 16:28:39
本文介绍了从数据库值填充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

本文发布于:2023-08-05 20:54:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1307996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   Django   ChoiceField

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!