如何从外键模型中填入Django中的下拉列表 [英] how to populate dropdown list in Django from a foreignkey model

查看:103
本文介绍了如何从外键模型中填入Django中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型。

class ArticleCategory(models.Model):
    category = models.CharField(max_length=255,blank=False)

class Article(models.Model):
    title = models.CharField(max_length=255,blank=False)
    body = models.TextField()
    pub_date = models.DateTimeField(auto_now_add=True)
    category = models.ForeignKey(ArticleCategory,default=1)

现在我必须渲染模板并保存文章模型的表单。我的文章模型中有一个foreignKey字段,因为我无法保存我的文章。我想从下拉列表中选择一个类别并将其保存在我的文章模型中。

Now I have to render a template and save the form for Article model. I have a foreignKey field in my Article Model and because of that I'm not able to save my article form. I want to select a category from dropdown list and save it in my Article model.

我应该如何为此模板进行代码编写?

How should I code my template for this ?

我的views.py函数是:

My views.py function for this is:

def create(request):
    if request.POST:
        form = ArticleForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/articles/all/')
    else:
        form = ArticleForm()
        args={}
        args.update(csrf(request))
        args['categories'] = ArticleCategory.objects.all()
        args['form'] = form
        return render_to_response('create_article.html', args)

我的模板create_article.html目前看起来像这样:

My template create_article.html currently looks like this:

<form role="form" action="/articles/create/" method="POST" enctype="multipart/form-data">{% csrf_token %}
                        <div class="row">
                            <div class="form-group col-lg-3">
                            <label></label>
                                <p>{{form.title}}</p>
                            </div>
                            <div class="form-group col-lg-3">
                                <label>Category</label>
                                <p>
                                    <select id="id_category">
                                        {% for category in categories  %}
                                        <option value="{{ category }}">{{ category.category }}</option>
                                        {% endfor %}
                                    </select>
                                </p>
                            </div>
                            <div class="clearfix"></div>
                            <div class="form-group col-lg-12">
                                <label>Body</label>
                                {{form.body}}
                            </div>
                            <div class="form-group col-lg-12">
                                <button type="submit" class="btn btn-default">Save Article</button>
                            </div>
                        </div>
                    </form>


推荐答案

您不需要手动执行。如果您的 ArticleForm ModelForm 并且不排除类别然后你可以写 {{form.category}} 并自动获取由django创建的下拉列表。它使用 ModelChoiceField 在下面。

You don't need to do this manually. If your ArticleForm is ModelForm and doesn't exclude category field then you can just write {{ form.category }} and get dropdown created by django automatically. It uses ModelChoiceField underneath the hood.

这篇关于如何从外键模型中填入Django中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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