Django 依赖下拉菜单

时间:2021-05-28 08:31:14

标签: django django-models django-rest-framework django-views django-forms

型号名称服务

class Service(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    serv_type = models.ForeignKey(Servicetype,on_delete=models.CASCADE)
    serv_job_card_no = models.CharField(max_length=100)
    serv_job_loc = models.CharField(max_length=100,default='')
    serv_cost = models.CharField(max_length=100)
    serv_exp_date = models.CharField(max_length=100)

型号名称服务类型。它们都是外键连接的服务和服务类型

class Servicetype(models.Model):
    serv_type = models.CharField(max_length=100)

获取数据的控制器:它从 servicetype 模型返回 int 值而不是 serv_type 文本值

def additionalService_getservtype(request):
    if request.method == 'POST':
        job_id = request.POST.get('job_id')
        print('----- job id ' + job_id)
        print(job_id)
        results = Service.objects.filter(serv_job_card_no=job_id).select_related('service').values('serv_type')
        # return render(request, 'services/ddservicename.html', {'results': results})
        data = list(results)
        print(data)
        return JsonResponse(data, safe=False)

2 个下拉菜单的 Html 代码...

<label for="">Select Job No. (Step 2)</label>
<select required class="form-select" name="a_serv_job_card_no" id="a_serv_job_card_no">
<option value="" selected="true" disabled="disabled">----------</option>
</select>
<label for="">Select Service Name ( Step 3 )</label>
<select required class="form-select" name="a_serv_type" id="a_serv_type">
<option value="" disabled selected>----------</option>
</select>

Ajax 调用以获取下拉服务类型的数据

$('#a_serv_job_card_no').on('change', function () {
var a_serv_job_card_no_val = this.options[this.selectedIndex].text
console.log(a_serv_job_card_no_val)
let csr = $("input[name=csrfmiddlewaretoken]").val();
$.ajax({
url: "{% url 'additionalservice_getservtype' %}",
method: "POST",
dataType: "json",
data: { job_id: a_serv_job_card_no_val, csrfmiddlewaretoken: csr },
                        success: function (data) {
                            // $("#a_serv_type_modal").html(data)
        
                            if (data.length > 0) {
                                $("#a_serv_type").empty()
                                var rows = null
                                for (var i = 0; i < data.length; i++) {
                                    var rows = $('<option>' + data[i].serv_type + '</option>');
                                    $("#a_serv_type").append(rows);
                                }
                                $('#a_serv_type').html(d)
                            }
        
                            else {
                                $("#a_serv_type").empty()
                                var rows = $('<option disabled>' + " No data" + '</option>');
                                $("#a_serv_type").append(rows);
                            }
                        }
                    })
                });

以上是ajax调用。不是我想调用这个 ajax 调用并从 servicetype 获取数据意味着获取服务类型名称,但现在我正在获取 int 值,因为我想要 serv_type 文本值...

我做错了什么?

0 个答案:

没有答案