0

我目前正在学习 Django。假设一个简单的情况:数据库中有两张表:Region->Country(1-多关系,Region是Country的外键)

所以当我们创建一个新国家时,我们必须为一个国家指定地区,但似乎地区必须是一个对象而不是简单的名称(字符串)。

我将如何更改我的 html 代码?

        <div class="field">
            {{ form.region.errors }}
            <label for="id_region">Region:</label>
            <select id="id_region" name="region">
                <option value="asia">Asia</option>
                <option value="europe">Europe</option>
            </select>
            {{ form.region }}
        </div>

我想我应该对选择进行一些更改,但是我应该在这里创建对象吗?真的很困惑,非常感谢!

4

2 回答 2

0

This has nothing at all to do with your HTML code: it is all to do with how you process the form in the view.

You clearly have a Django form: that form will already construct and output the option values for the region field. Hard-coding them is pointless.

于 2013-05-28T08:10:38.577 回答
0

您需要Region先将区域添加到模型中,然后在添加Country. 我希望你有这两个模型。从你的问题看不清楚。

于 2013-05-28T08:06:27.400 回答