Django REST Framework can't show translatable fields

809 views Asked by At

I'm implementing a REST API for a Django-based website using the Django REST framework and Django-hvad apps.

Problem: translated fields in REST Viewset cause an error.

Please note: another question addressed & solved a similar issue, but it does not apply to the app versions I've installed, that is:

  1. Django 1.8
  2. hvad 1.2.0 which has-or rather claims to have- builtin support for REST framework, as explained here
  3. Django Rest Framework 3.1.1

Here is my model:

class Website(TranslatableModel):

    name = models.CharField(max_length=100,unique=True)
    default_url = models.URLField(max_length=100,unique=True)
    created = models.DateTimeField(auto_now_add=True)

    translations = TranslatedFields(
        url = models.URLField(max_length=100, unique=True),
        description = models.TextField(null=True,blank=True),
    )

And my urls.py looks like this (I took the imports out for brevity):

# Serializer
class WebsiteSerializer(HyperlinkedTranslatableModelSerializer):
    class Meta:
        model = Website
        fields = ('name','default_url','url','description')

# Viewset
class WebsiteViewSet(viewsets.ModelViewSet):
    queryset = Website.objects.language().all()
    serializer_class = WebsiteSerializer

# REST Router
router = routers.DefaultRouter()
router.register(r'websites', WebsiteViewSet)


# URL Patterns
urlpatterns = patterns( '', 
    # ... non translated urls
)

urlpatterns += i18n_patterns('',

    url(r'^api/v1/', include('rest_framework.urls', namespace='rest_framework')),
    url(r'^api/v1/', include(router.urls)),
    url(r'^admin/', include(admin.site.urls)),
)

With these, the URL /it/api/v1/websites/ gives the annoying error:

WrongManager at /it/api/v1/websites/ To access translated fields like 'url' from an untranslated model, you must use a translation aware manager. For non-translatable models, you can get one using hvad.utils.get_translation_aware_manager. For translatable models, use the language() method.

Note: replacing the /it prefix with any other language code doesn't help in any way.

What I've tried so far:

  1. in WebsiteViewSet class, specifying a fixed argument to the language() function.
  2. using a TranslationMixin (a language-agnostic serializer exposing all the translations), following the instructions here. I changed the queryset line like so: queryset = Website.objects.untranslated().prefetch_related('translations').all()
  3. moving the api url definitions outside of the i18n patterns

But in all cases it always gives the very same error.

What am I missing?

0

There are 0 answers