sandeep
sandeep

Reputation: 711

Apply filter with Prefetch in prefetch_related()

I am creating a multi language project. I am using 2 models to save data. Fields that are same for all languages (like images) in model 1 and texts in model 2 with ForeignKey to model 1.

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')

There is a default language. All contents will be available in default language (french). If user select another language, I have search content in that language and if content is not available in that language, I have to show content in default language. I using this query.

fidelities = FidelityClubs.objects.all().prefetch_related(Prefetch('texts', FidelityClubTexts.objects.filter(language__code=lang_code)))

I am sending this data by APIs. This is showing data like this.

{
            "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&#39;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": []
        },

This is not working. If data is available in required language then it's okay, but if not it's showing nothing. How can I replace blank data with data in default language.

If I use to_attrs in Prefetch like this

fidelities = FidelityClubs.objects.all().prefetch_related(Prefetch('texts', FidelityClubTexts.objects.filter(language__code=lang_code), to_attr='l_texts'))

It's showing data in all languages.

{
            "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&#39;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&#39;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&#39;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
                }
            ]
        },

Can anyone help?

Upvotes: 0

Views: 83

Answers (1)

Radwan Abu-Odeh
Radwan Abu-Odeh

Reputation: 2055

There is an easier solution for your case, I strongly recommend you to use Django Model Translation library. the cool thing about it is that it relies request's Accept-Language header, which will return your text based on the passed language code in the header, and if it is not there it will fallback to a default language code you set.

Upvotes: 1

Related Questions