django-dataTables无法使用外部/ manyToMany引用进行搜索

时间:2019-01-15 10:39:47

标签: django datatables django-rest-framework

我使用的是Django的dataTables rest框架,当我尝试使用路由从头开始搜索数据并且使用manyToMany或外键引用对此路由进行搜索时,这会导致icontains问题

http://127.0.0.1:8000/test/api/tout-les-ig/?format=datatables&draw=4&columns[0][data]=number&columns[0][name]=&columns[0][searchable]=true&columns[0][orderable]=true&columns[0][search][value]=test&columns[0][search][regex]=false&order[0][column]=0&order[0][dir]=asc&start=0&length=90&search[value]=&search[regex]=false&_=1547546595865

让我们说[data] =数字是外键,[search] [value] = test 因此,如果我不输入[search] [value] =一个值,就不会出错 但是如果我愿意,我会说:“相关字段的查找无效:​​icontains”

我已经准备尝试在序列化程序中执行以下所有操作:     数字= serializers.SerializerMethodField()     def get_style(self,IG):         返回','.join([SOME.number.all()中nb的str(,b.get_number()))

JS文件:

string a = "-1"; // These are values that are configurable based on which date is checked. Yesterday means, -1 for example. 
string b = "-15"; // -15 means within last 15 days.
DateTime d = new DateTime();
DateTime e = d.AddDays(int.Parse(a));
if (DateTime.Now.Date >= d.Date && e.Date <= d.Date)
{

}

序列化器: IGSerializer类(serializers.ModelSerializer):

      var table = $('#liste').DataTable(
      {
          "language": {
                        "emptyTable":           "Aucune donnée trouvée dans la base",
                        "sSearch":              "recherche: ",
                        "searchPlaceholder":    "recherche dans IGIPOP",
                        "lengthMenu":           "Affichage de _MENU_ éléments par page",
                        "infoEmpty":            "Aucun éléments ne correponds à la recherche",
                        "info":                 "résultats de _START_ à _END_ pour un total de _TOTAL_",
                        "infoFiltered":         "(filtré avec _MAX_ éléments trouvés)",
                        "zeroRecords":          "Aucun résultat",
                        "processing":           "Recherche en cours...",
                        "paginate": {
                                        "previous": "Page précédente",
                                        "next":     "Page suivante"
                                    }
                      },


          'serverSide': true, // permet les requêtes ajax
          'ajax': '{{api_route}}', // route du json attendu
              dataFilter: function(data){
                var json = jQuery.parseJSON(data);
                json.recordsTotal = json.count;
                json.recordsFiltered = json.count;
                json.data = json.results;
                delete json.results;
                return JSON.stringify(json); // return JSON string
            },
          'columns': [ 

            {% for data in datas %}
                {% if data == 'numero' %}
                {
                    'data':'{{data}}',
                    "fnCreatedCell": function(nTd, sData, oData, iRow, iCol)
                        {
                            $(nTd).html("<a href=" + "\"" + oData.og + "/\"" + "><span class='btn btn-success'>" + oData.numero + "</span></a>");
                        },
                },
                {% else %}
                    {'data':'{{data}}'},
                {% endif %}
            {% endfor %}
          ],

          "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull)
          {
            $(nRow).css('background-color', aData.style['background-color']);
            $(nRow).css('color', aData.style['color']);

            return nRow;
          },
          deferRender:    true,
          scrollY:        '50vh',
          scrollCollapse: true,
          scroller:       true,
          stateSave:      true,
          "processing":   true,
    }
    );

0 个答案:

没有答案