我在Django项目中为我的表使用DataTables。现在,它如下:
我的DataTables目前看起来像这样:
现在,我想要的是以下内容:
我现在所知道的是,使用python,我可以使用current_user.organizations.all()
获取所有用户的组织,因为models.py的related_name属性设置为'organizations'
且组织已连接通过ManytoMany字段的用户模型如下:
#models.py
class Organization(SlugModel, RandomIDModel):
users = models.ManyToManyField('accounts.User',
through='OrganizationRole',
related_name='organizations')
但我不知道如何在DataTables中完成此任务。如何进行?请帮忙。
以下是将表元素初始化为DataTable所需的JS:
$('.datatable').DataTable({
conditionalPaging: true,
"dom": '<"table-search clearfix"f>t<"table-entries"i><"table-num"l><"table-pagination"p>',
"language": {
"emptyTable": "{% trans "No data available in table" %}",
"info": "{% trans "Showing _START_ - _END_ of _TOTAL_" %}",
"infoEmpty": "{% trans "Showing 0 - 0 of 0" %}",
"infoFiltered": "{% trans "(filtered from _MAX_ total rows)" %}",
"lengthMenu": "{% trans "Show _MENU_ rows" %}",
"search": '<div class="input-group"><span class="input-group-addon"><span class="glyphicon glyphicon-search"></span></span>',
"searchPlaceholder": '{% trans "Search" %}',
"zeroRecords": "{% trans "No matching records found" %}",
"paginate": {
"next": '<span class="glyphicon glyphicon-triangle-right"></span>',
"previous": '<span class="glyphicon glyphicon-triangle-left"></span>',
"first": "{% trans "First" %}",
"last": "{% trans "Last" %}"
},
"aria": {
"sortAscending": ": {% trans "activate to sort column ascending" %}",
"sortDescending": ": {% trans "activate to sort column descending" %}"
}
},
});
HTML模板:
<!-- Organization index table -->
<table class="table table-hover datatable" data-paging-type="simple">
<thead>
<tr>
<th class="col-md-10">{% trans "Organization" %}</th>
<th class="col-md-2 hidden-xs">{% trans "Projects" %}</th>
<th class="hidden"><!-- Hidden archived status column --></th>
</tr>
</thead>
{% for org in object_list %}
<tr class="linked" onclick="window.document.location='{% url 'organization:dashboard' slug=org.slug %}';">
<td>
{% if org.logo %}
<div class="org-logo">
<span class="hidden">{{ org.name }}</span><!-- needed for sorting -->
<img src="{{ org.logo }}" class="org-logo" alt="{{ org.name }}"/>
</div>
{% endif %}
<div class="org-text">
<h4><a href="{% url 'organization:dashboard' slug=org.slug %}">{{ org.name }}</a>
{% if org.archived %}
<span class="label label-danger">{% trans "Archived" %}</span>
{% endif %}
</h4>
{% if org.description %}
<p>{{ org.description }}</p>
{% endif %}
</div>
</td>
<td class="hidden-xs">{{ org.num_projects }}</td>
<td class="hidden" data-filter="archived-{{ org.archived }}"></td>
</tr>
{% endfor %}
</table>
请询问是否需要其他信息。非常感谢。