Python django datatables多字段服务器端处理
在我的Django项目中,我使用datatables.net jquery插件在表中显示来自Django模型的一些数据。我正在使用来执行服务器端处理。一切正常,但我必须显示另一个表中的一些数据,该表使用Django的manytomy字段链接到第一个表。 当前,它在datatable的每一行中显示“appname.Location.None” 型号Python django datatables多字段服务器端处理,python,django,django-models,datatables,django-datatable,Python,Django,Django Models,Datatables,Django Datatable,在我的Django项目中,我使用datatables.net jquery插件在表中显示来自Django模型的一些数据。我正在使用来执行服务器端处理。一切正常,但我必须显示另一个表中的一些数据,该表使用Django的manytomy字段链接到第一个表。 当前,它在datatable的每一行中显示“appname.Location.None” 型号 class Location(models.Model): location = models.CharField(max_length=20
class Location(models.Model):
location = models.CharField(max_length=200000, blank=True, null=True)
lat = models.FloatField(blank=True, null=True)
long = models.FloatField(blank=True, null=True)
def __str__(self):
return self.location
class Event(models.Model):
name = models.CharField(max_length=200, blank=True, null=True)
is_active = models.BooleanField(default=True)
status = models.CharField(max_length=100, blank=True, null=True)
types = models.CharField(max_length=500000, blank=True, null=True)
impact = models.CharField(max_length=500000, blank=True, null=True)
loc = models.ManyToManyField(to=Location, related_name='loc', blank=True)
def __str__(self):
return self.name
class events(BaseDatatableView):
columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
order_columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
def get_initial_queryset(self):
return Event.objects.filter(is_active=True)
class events(BaseDatatableView):
columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
order_columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
def get_initial_queryset(self):
return Event.objects.filter(is_active=True)
def prepare_results(self, qs):
data = []
for item in qs:
loc = []
for loca in item.loc.all():
loc.append(loca.location)
loc = ' '.join(loc)
data.append([
item.name,
item.status,
item.impact,
item.types,
item.id,
loc
])
return data
视图
class Location(models.Model):
location = models.CharField(max_length=200000, blank=True, null=True)
lat = models.FloatField(blank=True, null=True)
long = models.FloatField(blank=True, null=True)
def __str__(self):
return self.location
class Event(models.Model):
name = models.CharField(max_length=200, blank=True, null=True)
is_active = models.BooleanField(default=True)
status = models.CharField(max_length=100, blank=True, null=True)
types = models.CharField(max_length=500000, blank=True, null=True)
impact = models.CharField(max_length=500000, blank=True, null=True)
loc = models.ManyToManyField(to=Location, related_name='loc', blank=True)
def __str__(self):
return self.name
class events(BaseDatatableView):
columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
order_columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
def get_initial_queryset(self):
return Event.objects.filter(is_active=True)
class events(BaseDatatableView):
columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
order_columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
def get_initial_queryset(self):
return Event.objects.filter(is_active=True)
def prepare_results(self, qs):
data = []
for item in qs:
loc = []
for loca in item.loc.all():
loc.append(loca.location)
loc = ' '.join(loc)
data.append([
item.name,
item.status,
item.impact,
item.types,
item.id,
loc
])
return data
脚本
$(document).ready(function () {
$('#tableid').DataTable({
"processing": true,
"serverSide": true,
"ajax": "{% url 'evnts' %}", //goes to class evnts
columns: [
{
data: 'name',
},
{
data: 'status',
},
{
data: 'impact',
},
{
data: 'types',
},
{
data: 'id',
},
{
data: 'loc'
}
]
});
});
我做错了什么?我必须稍微改变一下我的视图和脚本,我才能让它工作 视图
class Location(models.Model):
location = models.CharField(max_length=200000, blank=True, null=True)
lat = models.FloatField(blank=True, null=True)
long = models.FloatField(blank=True, null=True)
def __str__(self):
return self.location
class Event(models.Model):
name = models.CharField(max_length=200, blank=True, null=True)
is_active = models.BooleanField(default=True)
status = models.CharField(max_length=100, blank=True, null=True)
types = models.CharField(max_length=500000, blank=True, null=True)
impact = models.CharField(max_length=500000, blank=True, null=True)
loc = models.ManyToManyField(to=Location, related_name='loc', blank=True)
def __str__(self):
return self.name
class events(BaseDatatableView):
columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
order_columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
def get_initial_queryset(self):
return Event.objects.filter(is_active=True)
class events(BaseDatatableView):
columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
order_columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
def get_initial_queryset(self):
return Event.objects.filter(is_active=True)
def prepare_results(self, qs):
data = []
for item in qs:
loc = []
for loca in item.loc.all():
loc.append(loca.location)
loc = ' '.join(loc)
data.append([
item.name,
item.status,
item.impact,
item.types,
item.id,
loc
])
return data
我必须将列
替换为
columnDefs: [
{
targets: '0',
name: 'name',
},
{
targets: '1',
name: 'status',
},
{
targets: '2',
name: 'impact',
},
{
targets: '3',
name: 'types',
},
{
targets: '4',
name: 'id',
},
{
targets: '5',
name: 'loc',
}
]
在我的脚本中
而targets指的是表的
标记的类名。
示例
<th class="0">Name</th>
名称
我必须稍微改变一下我的视图和脚本,我才能让它工作
视图
class Location(models.Model):
location = models.CharField(max_length=200000, blank=True, null=True)
lat = models.FloatField(blank=True, null=True)
long = models.FloatField(blank=True, null=True)
def __str__(self):
return self.location
class Event(models.Model):
name = models.CharField(max_length=200, blank=True, null=True)
is_active = models.BooleanField(default=True)
status = models.CharField(max_length=100, blank=True, null=True)
types = models.CharField(max_length=500000, blank=True, null=True)
impact = models.CharField(max_length=500000, blank=True, null=True)
loc = models.ManyToManyField(to=Location, related_name='loc', blank=True)
def __str__(self):
return self.name
class events(BaseDatatableView):
columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
order_columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
def get_initial_queryset(self):
return Event.objects.filter(is_active=True)
class events(BaseDatatableView):
columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
order_columns = ['name', 'status', 'impact', 'types', 'id', 'loc']
def get_initial_queryset(self):
return Event.objects.filter(is_active=True)
def prepare_results(self, qs):
data = []
for item in qs:
loc = []
for loca in item.loc.all():
loc.append(loca.location)
loc = ' '.join(loc)
data.append([
item.name,
item.status,
item.impact,
item.types,
item.id,
loc
])
return data
我必须将列
替换为
columnDefs: [
{
targets: '0',
name: 'name',
},
{
targets: '1',
name: 'status',
},
{
targets: '2',
name: 'impact',
},
{
targets: '3',
name: 'types',
},
{
targets: '4',
name: 'id',
},
{
targets: '5',
name: 'loc',
}
]
在我的脚本中
而targets指的是表的
标记的类名。
示例
<th class="0">Name</th>
名称