django 数据表 manytomany 字段服务器端处理
django datatables manytomany field serverside processing
在我的 Django 项目中,我使用 datatables.net jquery 插件在 table 中显示来自我的 Django 模型的一些数据。我正在使用 this 进行服务器端处理。一切正常,但我必须使用 Django 的 manytomany 字段显示链接到第一个 table 的另一个 table 的一些数据。
目前它在数据的每一行中显示 'appname.Location.None'table。
型号
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)
脚本
$(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 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
我必须用
替换columns
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 指的是 table 的 <th>
标签的 class 名称。
例子
<th class="0">Name</th>
在我的 Django 项目中,我使用 datatables.net jquery 插件在 table 中显示来自我的 Django 模型的一些数据。我正在使用 this 进行服务器端处理。一切正常,但我必须使用 Django 的 manytomany 字段显示链接到第一个 table 的另一个 table 的一些数据。 目前它在数据的每一行中显示 'appname.Location.None'table。
型号
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)
脚本
$(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 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
我必须用
替换columns
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 指的是 table 的 <th>
标签的 class 名称。
例子
<th class="0">Name</th>