Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

duplicating translation in admin.save_model shows blank translations afterwards #353

Open
gfavre opened this issue Jun 6, 2018 · 0 comments
Labels

Comments

@gfavre
Copy link

gfavre commented Jun 6, 2018

Hi,
I noticed an issue in hvad admin (v. 1.8.0)
General usage scenario for me is:

  1. we serve documents in 4 languages
  2. better serving a copy of one language into another than nothing.
  3. I need to show all translations with django rest framework (works perfectly)

(Simplified) model:

from django.db import models

from hvad.models import TranslatableModel, TranslatedFields


class BindingDocument(TranslatableModel):
    translations = TranslatedFields(
        document=models.FileField(),
        label=models.CharField(_("Document label"), max_length=255),
    )

admin:

from django.contrib import admin
from django.conf import settings

from hvad.admin import TranslatableAdmin

from .models import BindingDocument


@admin.register(BindingDocument)
class BindingDocumentAdmin(TranslatableAdmin):
    list_display = ('all_translations',)
    
    def get_queryset(self, request):
        return BindingDocument.objects.prefetch_related('translations')

    def save_model(self, request, obj, form, change):
        super().save_model(request, obj, form, change)
        document = obj.document
        label = obj.label

        for (language_code, _) in settings.LANGUAGES:
            if obj.translations.filter(language_code=language_code).exists():
                continue
            obj.translate(language_code)
            obj.document = document
            obj.label = label
            obj.save()

Database is filled correctly (i.e. one translation per language is created, all contain same data), rest framework (using TranslationsMixin) also works perfectly.
Admin shows all languages in list view, as expected. However when going to any detail page other than the language in which translation was created, the result is blank. When entering content in blank fields, I get a duplicate key value violates unique constraint "products_bindingdocument_language_code_master_id_add98726_uniqexception.

My guess is that it has something to do with cache internals. Looks like a bug to me, perhaps it has a workaround?

@spectras spectras added the bug label Oct 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants