Django 在预回迁相关()中使用预回迁应用筛选器
我正在创建一个多语言项目。我使用2个模型来保存数据。对于模型1中的所有语言(如图像)和模型2中的文本都相同的字段,并将ForeignKey设置为模型1Django 在预回迁相关()中使用预回迁应用筛选器,django,Django,我正在创建一个多语言项目。我使用2个模型来保存数据。对于模型1中的所有语言(如图像)和模型2中的文本都相同的字段,并将ForeignKey设置为模型1 class FidelityClubs(models.Model): image = models.ImageField(upload_to='admin/fidelity_clubs/') class FidelityClubTexts(models.Model): title = models.CharField(max_
class FidelityClubs(models.Model):
image = models.ImageField(upload_to='admin/fidelity_clubs/')
class FidelityClubTexts(models.Model):
title = models.CharField(max_length=255)
content = RichTextField()
language = models.ForeignKey('van_admin.Languages', on_delete=models.CASCADE, default='1')
club = models.ForeignKey('van_admin.FidelityClubs', on_delete=models.CASCADE, related_name='texts')
有一种默认语言。所有内容将以默认语言(法语)提供。如果用户选择另一种语言,则我有该语言的搜索内容,如果该语言的内容不可用,则我必须以默认语言显示内容。我正在使用这个查询
fidelities = FidelityClubs.objects.all().prefetch_related(Prefetch('texts', FidelityClubTexts.objects.filter(language__code=lang_code)))
我通过API发送这些数据。这是显示这样的数据
{
"image": "http://127.0.0.1:8000/media/admin/fidelity_clubs/logo-1.png",
"texts": [
{
"id": 9,
"title": "CosmetiCar en",
"content": "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, CosmetiCar Informaton Lorem Ipsum is simply dummy text of the printing and typesetting industry. en</p>",
"language": 2,
"club": 13
}
]
},
{
"image": "http://127.0.0.1:8000/media/admin/fidelity_clubs/logo-2.png",
"texts": []
},
它以各种语言显示数据
{
"image": "http://127.0.0.1:8000/media/admin/fidelity_clubs/logo-1.png",
"texts": [
{
"id": 1,
"title": "CosmetiCar fr",
"content": "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, CosmetiCar Informaton Lorem Ipsum is simply dummy text of the printing and typesetting industry. fr</p>",
"language": 1,
"club": 13
},
{
"id": 9,
"title": "CosmetiCar en",
"content": "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, CosmetiCar Informaton Lorem Ipsum is simply dummy text of the printing and typesetting industry. en</p>",
"language": 2,
"club": 13
}
]
},
{
"image": "http://127.0.0.1:8000/media/admin/fidelity_clubs/logo-2.png",
"texts": [
{
"id": 2,
"title": "Brandrup fr",
"content": "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, CosmetiCar Informaton Lorem Ipsum is simply dummy text of the printing and typesetting industry. fr</p>",
"language": 1,
"club": 14
}
]
},
{
“图像”:http://127.0.0.1:8000/media/admin/fidelity_clubs/logo-1.巴布亚新几内亚“,
“案文”:[
{
“id”:1,
“头衔”:“化妆师fr”,
“内容”:“Lorem Ipsum只是印刷和排版行业的虚拟文本。自15世纪以来,Lorem Ipsum一直是该行业的标准虚拟文本,CosmetiCar Informaton Lorem Ipsum只是印刷和排版行业的虚拟文本。fr”,
“语言”:1,
“俱乐部”:13
},
{
“id”:9,
“头衔”:“美容师”,
“内容”:“Lorem Ipsum只是印刷和排版行业的虚拟文本。自15世纪以来,Lorem Ipsum一直是该行业的标准虚拟文本,CosmetiCar Informaton Lorem Ipsum只是印刷和排版行业的虚拟文本。en”,
“语言”:2,
“俱乐部”:13
}
]
},
{
“图像”:http://127.0.0.1:8000/media/admin/fidelity_clubs/logo-2.巴布亚新几内亚“,
“案文”:[
{
“id”:2,
“标题”:“Brandrup fr”,
“内容”:“Lorem Ipsum只是印刷和排版行业的虚拟文本。自15世纪以来,Lorem Ipsum一直是该行业的标准虚拟文本,CosmetiCar Informaton Lorem Ipsum只是印刷和排版行业的虚拟文本。fr”,
“语言”:1,
“俱乐部”:14
}
]
},
有人能帮忙吗?对于您的情况,有一个更简单的解决方案,我强烈建议您使用该库。最酷的是它依赖于请求的
Accept Language
头,它将根据头中传递的语言代码返回文本,如果没有,它将返回到您设置的默认语言代码
{
"image": "http://127.0.0.1:8000/media/admin/fidelity_clubs/logo-1.png",
"texts": [
{
"id": 1,
"title": "CosmetiCar fr",
"content": "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, CosmetiCar Informaton Lorem Ipsum is simply dummy text of the printing and typesetting industry. fr</p>",
"language": 1,
"club": 13
},
{
"id": 9,
"title": "CosmetiCar en",
"content": "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, CosmetiCar Informaton Lorem Ipsum is simply dummy text of the printing and typesetting industry. en</p>",
"language": 2,
"club": 13
}
]
},
{
"image": "http://127.0.0.1:8000/media/admin/fidelity_clubs/logo-2.png",
"texts": [
{
"id": 2,
"title": "Brandrup fr",
"content": "<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, CosmetiCar Informaton Lorem Ipsum is simply dummy text of the printing and typesetting industry. fr</p>",
"language": 1,
"club": 14
}
]
},