Django使用基于单选字段的模型数据自动填充整个表单

时间:2016-08-19 16:27:52

标签: django django-models django-forms

我正在尝试制作一个基于单个选择字段填充所有字段的模型。基本上,当用户从下拉列表中选择一个值时,它将根据数据库中的数据填充其余字段。

models.py

class Commands(models.Model):
    command_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)
    command_prefix = models.TextField()
    command = models.TextField()
    args = models.TextField()
    shell = models.TextField()
    role = models.ForeignKey('Roles', models.DO_NOTHING)
    os = models.ForeignKey('Operatingsystems', models.DO_NOTHING)
    job_type = models.ForeignKey('Jobtypes', models.DO_NOTHING)
    active = models.IntegerField()

views.py

@verified_email_required
def jobs(request):
    return render(request, 'backend/jobs.html', {'form': CommandsForm()})

forms.py

class CommandsForm(Form):
    name = ModelChoiceField(queryset=Commands.objects.filter(active=1).values('name'))
    os = CharField(required=True, disabled=True)
    command_prefix = CharField(required=True, disabled=True)
    target = CharField(required=True)
    command = CharField(required=True, disabled=True)
    args = CharField(required=True, disabled=True)
    shell = CharField(required=True, disabled=True)

urls.py

urlpatterns = [
    url(r'^$', profile, name='profile'),
    url(r'^jobs/$', jobs, name='jobs'),
    url(r'^patchreport/$', patchreport, name='patchreport'),
    url(r'^prtbl/$', PatchReportTable.as_view(), name='patchreptbl')
]

jobs.html

{% extends "backend/base.html" %}
{% load staticfiles %}
{% load widget_tweaks %}

{% block title %}
    {{block.super}}Jobs
{% endblock %}

{% block content %}
<form id="jobs_form" class="form-horizontal text-center" method="post" action="{% url 'jobs' %}">
  {% csrf_token %}
  {{ form.non_field_errors }}

  <div class="form-group">
    {{ form.name.errors }}
    <label for="{{ form.name.id_for_label }}" class="col-sm-2 control-label">Name</label>
    <div class="col-sm-8">
      {{ form.name|add_class:"form-control" }}
    </div>
  </div>

  <div class="form-group">
    <label for="{{ form.os.id_for_label }}" class="col-sm-2 control-label">Os</label>
    <div class="col-sm-8">
      {{ form.os|add_class:"form-control" }}
    </div>
  </div>

  <div class="form-group">
    <label for="{{ form.command_prefix.id_for_label }}" class="col-sm-2 control-label">Command prefix</label>
    <div class="col-sm-8">
      {{ form.command_prefix|add_class:"form-control" }}
    </div>
  </div>

  <div class="form-group">
    <label for="{{ form.target.id_for_label }}" class="col-sm-2 control-label">Target</label>
    <div class="col-sm-8">
      {{ form.target|add_class:"form-control" }}
    </div>
  </div>

  <div class="form-group">
    <label for="{{ form.command.id_for_label }}" class="col-sm-2 control-label">Command</label>
    <div class="col-sm-8">
      {{ form.command|add_class:"form-control" }}
    </div>
  </div>

  <div class="form-group">
    <label for="{{ form.args.id_for_label }}" class="col-sm-2 control-label">Args</label>
    <div class="col-sm-8">
      {{ form.args|add_class:"form-control" }}
    </div>
  </div>

  <div class="form-group">
    <label for="{{ form.shell.id_for_label }}" class="col-sm-2 control-label">Shell</label>
    <div class="col-sm-8">
      {{ form.shell|add_class:"form-control" }}
    </div>
  </div>

  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-8">
      <button type="submit" class="btn btn-default" name="submit">Submit</button>
    </div>
  </div>

</form>

<script type="text/javascript" src="{% static "js/jobs.js" %}"></script>
{% endblock %}

老实说,我不确定如何使用Django的ModelForms实现这一目标。目前,我接受了迈克尔·普拉特的建议,并在名为change的javascript文件中使用jobs.js事件上的javascript自动填充字段。我不得不相信有一种方法可以通过直接从数据库中填充整个表单来完成同样的事情,或者通过类似于TastyPie使用ajax调用生成的RESTful api。

1 个答案:

答案 0 :(得分:1)

如果你没有被使用javascript的概念所关闭,你可以为你的特定选择字段使用.change()事件。所以它看起来像这样:

$(document).ready(function(){
    $("#id_name").change(function() {
        // Find your different fields you want to populate and set the values here.
        // Example would be 
        if ($("#id_name").val() == "Some value") {
            $("#id_command_prefix").val("Whatever you want to populate with.")
        }
    });
});