JSON 数组的不同数据类型基于 Django Rest API 和 Django-admin 中是否为 Debug=True/False
Different data type for JSON array based whether Debug=True/False in Django Rest API and Django-admin
我在管理界面中使用 Django 的精彩 select2 下拉菜单来触发自定义 jQuery AJAX 对 DRF 端点的 GET 请求,以尝试填充基于外键关系。但是,当我在 settings.py 的生产环境 (Debug=False) 中对此进行测试时,每当我在开发中执行相同的请求时,它 returns 数据作为 Array() 对象而不是常规的 JSON 对象(调试=真)。
(function($){
$(document).ready(function () {
//Machine Cleaned drop-down box, Equipment cleaning log.
//// Hides the 'Add more' row for the Cleaning Entry table
$('table.cleaning-table tbody tr.add-row').remove();
row_container = $(
'<tr class="form-row dynamic-log_entry row1" id="log_entry-0">'+
'<td class="original"><input type="hidden" name="log_entry-0-id" id="id_log_entry-0-id">'+
'<input type="hidden" name="log_entry-0-log_entry" id="id_log_entry-0-log_entry"></td>'+
'<td class="field-cleaning_item"><input type="text" name="log_entry-0-cleaning_item" class="vForeignKeyRawIdAdminField" id="id_log_entry-0-cleaning_item">'+
'<a href="/admin/quality/equipmentaction/?_to_field=id" class="related-lookup" id="lookup_id_log_entry-0-cleaning_item" title="Lookup"></a></td>'+
'<td class="field-cleaning_action"><input type="text" name="log_entry-0-cleaning_action" disabled="" id="id_log_entry-0-cleaning_action" style="width: 200px;"></td>'+
'<td class="field-checked"><input type="checkbox" name="log_entry-0-checked" id="id_log_entry-0-checked"></td>'+
'<td class="field-na"><input type="checkbox" name="log_entry-0-na" id="id_log_entry-0-na"></td>'+
'<td class="field-grade"><select name="log_entry-0-grade" id="id_log_entry-0-grade">'+
'<option value="" selected="">---------</option>'+
'<option value="A">A - Pharmaceutical</option>'+
'<option value="B">B - Satisfactory</option>'+
'<option value="C">C - Unsatisfactory</option>'+
'<option value="D">D - N/A</option></select></td>'+
'<td class="field-notes"><input type="text" name="log_entry-0-notes" maxlength="512" id="id_log_entry-0-notes" class="vTextField"></td>'+
'<td class="delete"></td></tr>'
);
//// This binds an ".on(select)" event function to the select2 box for the machine_used that
//// preforms an AJAX call using the machine_fk reference id to get all the EquipmentActions
//// records for that machine using REST API.
$("select#id_machine_used").on("select2:select", function(event) {
machine_fk = event.params.data.id;
$.ajax({
contentType: 'application/json',
dataType:'json',
url:'http://192.168.254.13:8888/pyscales/v1/quality/?machine_fk='+machine_fk,
//Upon a successful GET request, data is returned in JSON form.
success: function(data) {
console.log(data);
$(data.results).each(function (i, item) {
// console.log(i, item);
});
}
});
});
});
})(django.jQuery);
我不明白是什么导致了这种情况,除了生产和开发之间可能与 jQuery 库发生冲突之外,但我正在使用 Whitenoise & Gunicorn 为 Django 提供来自同一文件夹的静态文件。我能看到的唯一区别是在生产中加载 jquery.min.js 而不是常规的 jquery.js 文件,这是预期的。
作为旁注,我还尝试将我的自定义脚本添加到管理中的媒体 class,而是决定通过管理模板中的标记进行硬编码,而不是覆盖管理模板。
似乎有很多方法可以完成这项工作,但我对 jQuery 和 AJAX 还很陌生,所以我觉得我错过了一些简单的东西,或者没有遵循 'best practice' 方法。我知道这有很多变数,所以如果我需要 post 其他东西,请告诉我。任何帮助将不胜感激,你们这些高水平的堆栈家伙是我的英雄!
作为 Django/DRF and/or jQuery 的未来观众和新人的一个教训,当类型发生变化时,请始终跟踪数据回到其来源,并仔细检查数据的细微变化处理 JSON 时的数组。
在尝试关注 "best practices" 时,我有 2 个 settings.py 文件:settings_dev.py 和 settings_prod.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'DEFAULT_PERMISSION_CLASSES' : [
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
)
}
一个包含 DRF 中的分页代码,一个没有。因此导致两个不同的 JSON 数组 return 基于我选择的 settings.py 文件...
//Production - w/o pagination
[
{
"id": 59,
"item_fk_id": 1,
"item_fk": "TEST1",
"action": "TEST ACTION!"
}
]
//Development with paging
{
"count": 6,
"next": null,
"previous": null,
"results": [
{
"id": 59,
"item_fk_id": 1,
"item_fk": "TEST1",
"action": "TEST ACTION!"
}
]
我在管理界面中使用 Django 的精彩 select2 下拉菜单来触发自定义 jQuery AJAX 对 DRF 端点的 GET 请求,以尝试填充基于外键关系。但是,当我在 settings.py 的生产环境 (Debug=False) 中对此进行测试时,每当我在开发中执行相同的请求时,它 returns 数据作为 Array() 对象而不是常规的 JSON 对象(调试=真)。
(function($){
$(document).ready(function () {
//Machine Cleaned drop-down box, Equipment cleaning log.
//// Hides the 'Add more' row for the Cleaning Entry table
$('table.cleaning-table tbody tr.add-row').remove();
row_container = $(
'<tr class="form-row dynamic-log_entry row1" id="log_entry-0">'+
'<td class="original"><input type="hidden" name="log_entry-0-id" id="id_log_entry-0-id">'+
'<input type="hidden" name="log_entry-0-log_entry" id="id_log_entry-0-log_entry"></td>'+
'<td class="field-cleaning_item"><input type="text" name="log_entry-0-cleaning_item" class="vForeignKeyRawIdAdminField" id="id_log_entry-0-cleaning_item">'+
'<a href="/admin/quality/equipmentaction/?_to_field=id" class="related-lookup" id="lookup_id_log_entry-0-cleaning_item" title="Lookup"></a></td>'+
'<td class="field-cleaning_action"><input type="text" name="log_entry-0-cleaning_action" disabled="" id="id_log_entry-0-cleaning_action" style="width: 200px;"></td>'+
'<td class="field-checked"><input type="checkbox" name="log_entry-0-checked" id="id_log_entry-0-checked"></td>'+
'<td class="field-na"><input type="checkbox" name="log_entry-0-na" id="id_log_entry-0-na"></td>'+
'<td class="field-grade"><select name="log_entry-0-grade" id="id_log_entry-0-grade">'+
'<option value="" selected="">---------</option>'+
'<option value="A">A - Pharmaceutical</option>'+
'<option value="B">B - Satisfactory</option>'+
'<option value="C">C - Unsatisfactory</option>'+
'<option value="D">D - N/A</option></select></td>'+
'<td class="field-notes"><input type="text" name="log_entry-0-notes" maxlength="512" id="id_log_entry-0-notes" class="vTextField"></td>'+
'<td class="delete"></td></tr>'
);
//// This binds an ".on(select)" event function to the select2 box for the machine_used that
//// preforms an AJAX call using the machine_fk reference id to get all the EquipmentActions
//// records for that machine using REST API.
$("select#id_machine_used").on("select2:select", function(event) {
machine_fk = event.params.data.id;
$.ajax({
contentType: 'application/json',
dataType:'json',
url:'http://192.168.254.13:8888/pyscales/v1/quality/?machine_fk='+machine_fk,
//Upon a successful GET request, data is returned in JSON form.
success: function(data) {
console.log(data);
$(data.results).each(function (i, item) {
// console.log(i, item);
});
}
});
});
});
})(django.jQuery);
我不明白是什么导致了这种情况,除了生产和开发之间可能与 jQuery 库发生冲突之外,但我正在使用 Whitenoise & Gunicorn 为 Django 提供来自同一文件夹的静态文件。我能看到的唯一区别是在生产中加载 jquery.min.js 而不是常规的 jquery.js 文件,这是预期的。 作为旁注,我还尝试将我的自定义脚本添加到管理中的媒体 class,而是决定通过管理模板中的标记进行硬编码,而不是覆盖管理模板。
似乎有很多方法可以完成这项工作,但我对 jQuery 和 AJAX 还很陌生,所以我觉得我错过了一些简单的东西,或者没有遵循 'best practice' 方法。我知道这有很多变数,所以如果我需要 post 其他东西,请告诉我。任何帮助将不胜感激,你们这些高水平的堆栈家伙是我的英雄!
作为 Django/DRF and/or jQuery 的未来观众和新人的一个教训,当类型发生变化时,请始终跟踪数据回到其来源,并仔细检查数据的细微变化处理 JSON 时的数组。
在尝试关注 "best practices" 时,我有 2 个 settings.py 文件:settings_dev.py 和 settings_prod.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'DEFAULT_PERMISSION_CLASSES' : [
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
)
}
一个包含 DRF 中的分页代码,一个没有。因此导致两个不同的 JSON 数组 return 基于我选择的 settings.py 文件...
//Production - w/o pagination
[
{
"id": 59,
"item_fk_id": 1,
"item_fk": "TEST1",
"action": "TEST ACTION!"
}
]
//Development with paging
{
"count": 6,
"next": null,
"previous": null,
"results": [
{
"id": 59,
"item_fk_id": 1,
"item_fk": "TEST1",
"action": "TEST ACTION!"
}
]