javascript - Django REST Framework 数据表中的外键值
问题描述
我密切关注Django REST Framework 中的这个 SOF 问题Foreign key value以及许多其他问题,并多次阅读此文档和类似内容https://datatables.net/manual/tech-notes/4,但尚未找到解决方案。
错误信息:
DataTables 警告:表 id=entrytable - 请求第 0 行第 9 列的未知参数“符号”。有关此错误的更多信息,请参阅http://datatables.net/tn/4
但是,当我在错误消息上单击“确定”时,正确的 symbol.name 最终会起作用。过去 2 天我试图找到解决此错误的方法,但不知道还能尝试什么。
我认为这些数据显示为字符串。一些行也将显示为空。尝试了 if 语句,但仍然没有修复。
如果我注释掉:
# symbol = serializers.CharField(source='symbol.name', read_only=True)
然后序列化程序将只显示外键但不显示错误消息。现在似乎是我的数据表 javascript 中的一个问题。但是,我已经尝试了他们的建议,但仍然没有运气。
serializers.py(在 serializers.py 中注释掉的所有内容都是尝试的解决方案)
class EntrySerializer(serializers.ModelSerializer):
symbol = serializers.CharField(source='symbol.name', read_only=True)
# symbol = serializers.SerializerMethodField('get_symbol_name')
#
# def get_symbol_name(self, obj):
# return obj.symbol.name
class Meta:
model = Entry
# fields = ('id', 'date', 'amount', 'price', 'fee', 'entry_type', 'reg_fee', 'transaction_id', 'trade', 'symbol', 'created_by')
fields = '__all__'
# depth = 1
entry_list.html
{% extends "dashboard/base.html" %}
{% load i18n %}
{% block content %}
<style>
table.dataTable tbody tr.odd.selected {
background-color:#acbad4
}
table.dataTable tbody tr.even.selected {
background-color:#acbad5
}
</style>
<!-- Page Heading -->
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h2 class="text-gray-800">{% block title %}{% trans "Imported Entries" %}{% endblock %}</h2>
<a role="button" class="btn btn-success" href="{% url 'import' %}"><i
class="fas fa-plus-circle"></i> Import New Entires</a>
</div>
<button id="countbutton">Count rows</button>
<button id="deletebutton">Delete rows</button>
<!-- Content Row -->
<div class="row">
<div class="col-md-12">
<table id="entrytable"
class="table-hover table display table-bordered"
align="center"
style="width:100%">
<thead>
<tr role="row">
<th>id</th>
<th>date</th>
<th>amount</th>
<th>price</th>
<th>fee</th>
<th>entry_type</th>
<th>reg_fee</th>
<th>transaction_id</th>
<th>trade</th>
<th>symbol</th>
<!-- <th>created_by</th>-->
</tr>
</thead>
</table>
</div>
</div>
{% endblock %}
{% block js %}
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/dataTables.bootstrap4.min.css"/>
<!--https://datatables.net/examples/server_side/select_rows.html-->
<!--<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.22/css/jquery.dataTables.min.css"/>-->
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.22/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.22/js/dataTables.bootstrap4.min.js"></script>
<script>
$(document).ready(function() {
// var selected = [];
var table = $('#entrytable').DataTable({
"order": [[ 0, "desc" ]],
"processing": true,
"ajax": "/api/entry/?format=datatables",
"columns": [
{
"data": "id",
"render": function ( data, type, row, meta ) {
return '<a type="button" class="" target="_blank" href="' + data + '">' + data + ' </a>';
}
},
{"data": "date"},
{"data": "amount"},
{"data": "price"},
{"data": "fee"},
{"data": "entry_type"},
{"data": "reg_fee"},
{"data": "transaction_id"},
{
"data": "trade",
"render": function ( data, type, row, meta ) {
if (data) {
return '<a type="button" target="_blank" class="" href="/trade/' + data + '"> ' + data + ' </a>';
} else {
// show nothing
}
return data;
}
},
{"data": "symbol"},
// {"data": "created_by"},
],
});
$('#entrytable tbody').on( 'click', 'tr', function () {
$(this).toggleClass('selected');
} );
$('#countbutton').click( function () {
alert( table.rows('.selected').data().length +' row(s) selected' );
} );
$('#deletebutton').click( function () {
table.row('.selected').remove().draw( false );
} );
} );
</script>
{% endblock %}
解决方案
我在阅读文档 10 次以上后找到了答案。我认为组织这个问题很有帮助。它还决定早上 6 点起床再试一次。现在是 8 点 18 分,我明白了!
entry_list.html
{
"data": "symbol",
"defaultContent": "",
},
推荐阅读
- java - 在spring boot中无法获取用户名
- amazon-web-services - 使用 CodePipeline 从单个 CloudFormation 模板批量创建资源
- html - HTML 5 输入类型 tel 在移动设备上添加下划线选项
- node.js - Expressjs 未从 msnodesqlv8 err 对象返回所有属性
- angular - 角度原生脚本中的图像缩放问题
- cmake - 如何修复“CMAKE_C_COMPILER 不是完整路径且未在 PATH 中找到”?
- java - 使用java拆分获取值
- react-redux - 不断不得不杀死工作进程来启动我的 Phoenix 应用程序
- swift - Swift 动态动画,后跟属性动画师
- java - 如何获取 Spectron 测试结果并将其传递给外部测试(例如,Ucumber-Junit 测试)