2016-08-19 52 views
0

我正在嘗試製作基於單個選擇字段填充所有字段的模型表單。基本上,當用戶從下拉列表中選擇一個值時,它會根據來自數據庫的數據填充剩餘的字段。Django使用基於單選字段的模型數據自動填充整個表單

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做到這一點。目前,我採取了Michael Platt的建議,並使用JavaScript在change事件中自動填充javascript字段,名爲jobs.js。我必須相信有一種方法可以通過直接從數據庫填充整個表單或通過類似TastyPie使用ajax調用生成的RESTful API來完成同樣的事情。

回答

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.") 
     } 
    }); 
}); 
+0

的數據由數據庫填充,這將使它所以我必須手動添加數據自己的權利? – beardedeagle

+1

如果您的意思是當用戶選擇一個選項時,字段中預期的默認數據由數據庫填充,那麼您需要手動將這些值編碼到javascript中。我認爲這個文檔也可以提供幫助,但我不確定它是否正是你想要的:https://docs.djangoproject.com/en/1.10/ref/forms/api/#dynamic-initial-值看起來你可以在表單中設置一些初始值,但基本上每次我加載頁面時都會改變一些我不得不以某種方式,形狀或形式使用Javascript的東西。 –

相關問題