Django表单ForeignKey数据库字段未显示为下拉列表 [英] Django form ForeignKey database field not as dropdown

查看:44
本文介绍了Django表单ForeignKey数据库字段未显示为下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ForeignKey字段的数据库模型.从这个模型中,我用

I have a database model like, with a ForeignKey field. From this model I created a form with

class Foobar(models.Model):
    foobar = models.ForeignKey(Foobar)
    bar = models.Charfield()
    foo = models.Charfield()

class FoobarForm():
    class Meta:
        model = Foobar
        fields = ['bar', 'foo']

如果我在以下字段中添加foobar:fields = ['foobar','foo','bar']在html中,它是一个下拉控件...但是我不会输入文本.

If I add foobar to fields like: fields = ['foobar', 'foo', 'bar'] In html it is a dropdown control... but I wont a text input field.

我该怎么做?

推荐答案

您在寻找这个吗?

from django import forms

class FoobarForm():
    class Meta:
        model = Foobar

    def __init__(self, *args, **kwargs):
        super(FoobarForm, self).__init__(*args, **kwargs)
        self.fields['foobar'].widget = forms.TextInput()

这篇关于Django表单ForeignKey数据库字段未显示为下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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