如何在自定义过滤时从DataTables中的数据库中获取数据?

时间:2017-03-28 17:39:49

标签: python django datatables datatables-1.10

我在Django项目中为我的表使用DataTables。现在,它如下:

  • 有组织(单独的组织模型)
  • 每个组织都有一个项目(具有组织模型外键的项目模型)
  • 组织模型与用户模型具有ManytoMany关系,即用户可以同时成为多个组织的成员。

我的DataTables目前看起来像这样:

enter image description here

现在,我想要的是以下内容:

  • 在我的DataTable标题中添加另一个下拉过滤器,其中包含选项"所有组织"和#34;我的组织"这将过滤表以提供相应的结果(即显示所有组织,用户是其成员):

enter image description here

  • 同样包含一个下拉列表,显示用户有权访问的所有项目:

enter image description here

我现在所知道的是,使用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>

请询问是否需要其他信息。非常感谢。

0 个答案:

没有答案