[英]django-rest-framework-datatables and Django Parler's translation field
I've got model with translated fields.我有带有翻译字段的模型。
class Device(TranslatableModel):
translations = TranslatedFields(name=models.CharField(max_length=100))
I made a serializer like:我做了一个序列化程序,如:
class DeviceSerializer(TranslatableModelSerializer):
translations = TranslatedFieldsField(shared_model=Device)
class Meta:
model = Device
fields = ('translations',)
It gives me nice JSON like it should.它给了我很好的 JSON,就像它应该的那样。
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"device": {
"translations": {
"en": {
"name": "Sample Device"
}
}
}
}
]
}
Now i want to use it with django-rest-framework.现在我想将它与 django-rest-framework 一起使用。 In my template I've written script like:
在我的模板中,我编写了如下脚本:
$('#devices').DataTable({
'serverSide': true,
'ajax': 'api/devices/?format=datatables',
'columns': [
{'data':'device.translations.en'}
It refuses to work with me.它拒绝与我合作。 I am getting django.core.exceptions.FieldError: Unsupported lookup 'en' for AutoField or join on the field not permitted.
我收到 django.core.exceptions.FieldError: Unsupported lookup 'en' for AutoField or join on the field not allowed. If I am not appending .en to {'data'} it gives Object.object of course.
如果我没有将 .en 附加到 {'data'},它当然会给出 Object.object。
Issue is in template file.问题出在模板文件中。
Pass name & data field separately to columns in data-table configuration
将名称和数据字段分别传递给数据表配置中的列
please replace field_name
with your model field name请将
field_name
替换为您的模型字段名称
$('#devices').DataTable({
'ajax': 'api/devices/?format=datatables',
'columns': [
{"data": "translations.en.field_name" , "name": "translations.field_name"},
]
});
for more details refer django-rest-framework-datatables有关更多详细信息,请参阅django-rest-framework-datatables
& Django-parler-rest & Django-parler-rest
The actual problem is that while making get request to server data-table will add name value in column parameter so
实际问题是,在向服务器数据表发出 get 请求时会在列参数中添加名称值,因此
instead of writing
而不是写作
"name": "translations.en.field_name"
"name": "translations.en.field_name"
write down:
写下:
"name": "translations.field_name"
"name": "translations.field_name"
remove language code
删除语言代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.