Skip to content

Commit 2261938

Browse files
Merge pull request #2 from Mortrest/master
[BUGFIX] fix JS error and backend bug
2 parents 68e016e + 7d24f7e commit 2261938

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

django_dyn_dt/templates/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<!-- data-tables -->
99
<link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" type="text/css">
10-
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" type="text/javascript"></script>
10+
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@3.2.0" type="text/javascript"></script>
1111

1212
<!-- bootstrap5 -->
1313
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

django_dyn_dt/templates/static/src/controller/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export const exportData = (dataTable, type) => {
212212

213213
const searchParam = new URLSearchParams(window.location.search).get('search') || ''
214214

215-
const hiddenColumns = myData.headings.filter((d,i) => !dataTable.columns().visible(i))
215+
const hiddenColumns = myData.headings.filter((d,i) => !dataTable.columns.visible(i))
216216

217217
fetch (`/datatb/${modelName}/export/`,
218218
{method: 'POST',body: JSON.stringify({

django_dyn_dt/views.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from django.core.exceptions import ValidationError
55
from django.db.models import Q
6+
from django.db.models.fields.related import RelatedField
67
from django.http import HttpResponse
78
from django.shortcuts import render
89

@@ -22,18 +23,17 @@
2223
DYNAMIC_DATATB = {}
2324

2425
try:
25-
DYNAMIC_DATATB = getattr(settings, 'DYNAMIC_DATATB')
26-
except:
27-
pass
26+
DYNAMIC_DATATB = getattr(settings, 'DYNAMIC_DATATB')
27+
except:
28+
pass
2829

2930
# TODO: 404 for wrong page number
3031
def data_table_view(request, **kwargs):
3132
try:
3233
model_class = Utils.get_class(DYNAMIC_DATATB, kwargs.get('model_name'))
3334
except KeyError:
3435
return render(request, '404.html', status=404)
35-
headings = [field.name for field in model_class._meta.get_fields()]
36-
36+
headings = _get_headings(model_class)
3737
page_number = int(request.GET.get('page', 1))
3838
search_key = request.GET.get('search', '')
3939
entries = int(request.GET.get('entries', 10))
@@ -149,8 +149,7 @@ def export(request, **kwargs):
149149
export_type = request_body.get('type', 'csv')
150150
filter_options = Q()
151151

152-
headings = filter(lambda field: field.name not in hidden,
153-
[field for field in model_class._meta.get_fields()])
152+
headings = filter(lambda field: field.name not in hidden, _get_headings(model_class))
154153
headings = list(headings)
155154
for field in headings:
156155
field_name = field.name
@@ -230,3 +229,19 @@ def get_random_string(length):
230229
# choose from all lowercase letter
231230
letters = string.ascii_lowercase
232231
return ''.join(random.choice(letters) for i in range(length))
232+
233+
234+
def _get_headings(model_class, filter_relations=True):
235+
headings = []
236+
for field in model_class._meta.get_fields():
237+
if filter_relations and _is_relation_field(field):
238+
continue
239+
headings.append(field.name)
240+
return headings
241+
242+
def _is_relation_field(field):
243+
is_many_to_many_field = field.many_to_many is not None
244+
is_many_to_one_field = field.many_to_one is not None
245+
is_one_to_many_field = field.one_to_many is not None
246+
is_one_to_one_field = field.one_to_one is not None
247+
return is_many_to_many_field or is_many_to_one_field or is_one_to_many_field or is_one_to_one_field

0 commit comments

Comments
 (0)