I have a project setup with django 3.2.5 and drf 3.14.0 on python 3.6 and postgres 9.6.
I followed the following steps to create a translatable model for learning products model.
from translations.models import Translatable
class LearningProduct(Translatable):
title = models.CharField(
_("Title"),
max_length=255,
unique=True
)
description = models.TextField(
_("Description"),
blank=True,
null=True
)
image = models.ImageField(
_("Image"),
upload_to="uploads/images/learning_products/%Y/%m/%d",
max_length=255,
blank=True,
null=True
)
link = models.URLField(
_("Link"),
blank=True,
null=True
)
class Meta:
db_table = "learning_product"
verbose_name = _("Learning Product")
verbose_name_plural = _("Learning Products")
class TranslatableMeta:
fields = ['title', 'description', 'link']
def __str__(self):
return self.title
from translations.admin import TranslatableAdmin, TranslationInline
class LearningProductAdmin(TranslatableAdmin):
inlines = [TranslationInline]
admin.site.register(LearningProduct, LearningProductAdmin)
it lets me save the model's translations for the specified fields. i have two languages setup in my settings as:
## Translation settings.
LANGUAGES = (
('en', _('English')),
('ne', _('Nepali')),
)
i have my apiview setup as follows and it lets me fetch the model when no accept-language is passed as well as when En
is passed as the accept-language but i run into the error given below it when i try get method woth accept-language as Ne
.
views.py
# views.py
from rest_framework import generics
from core.models import LearningProduct
class LearningProductSerializer(serializers.ModelSerializer):
"""
Class to serialize Learning Product data types.
"""
class Meta:
model = LearningProduct
fields = '__all__'
class LearningProductListView(generics.ListAPIView):
queryset = LearningProduct.objects.all()
serializer_class = LearningProductSerializer
def get_queryset(self):
queryset = super(LearningProductListView, self).get_queryset()
return queryset.translate_related(
'title',
'description',
'link',
).translate()
[log].ERROR Internal Server Error: /api/v2/learningproducts/
Traceback (most recent call last):
File "C:\my-site\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\my-site\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\my-site\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\my-site\env\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\my-site\env\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\my-site\env\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\my-site\env\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\my-site\env\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\my-site\env\lib\site-packages\rest_framework\generics.py", line 199, in get
return self.list(request, *args, **kwargs)
File "C:\my-site\env\lib\site-packages\rest_framework\mixins.py", line 40, in list
page = self.paginate_queryset(queryset)
File "C:\my-site\env\lib\site-packages\rest_framework\generics.py", line 171, in paginate_queryset
return self.paginator.paginate_queryset(queryset, self.request, view=self)
File "C:\my-site\env\lib\site-packages\rest_framework\pagination.py", line 216, in paginate_queryset
return list(self.page)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\_collections_abc.py", line 883, in __iter__
v = self[i]
File "C:\my-site\env\lib\site-packages\django\core\paginator.py", line 188, in __getitem__
self.object_list = list(self.object_list)
File "C:\my-site\env\lib\site-packages\django\db\models\query.py", line 280, in __iter__
self._fetch_all()
File "C:\my-site\env\lib\site-packages\translations\querysets.py", line 54, in _fetch_all
with Context(self._result_cache, *self._trans_rels) \
File "C:\my-site\env\lib\site-packages\translations\context.py", line 21, in __init__
self.mapping, self.query = _get_purview(entity, hierarchy)
File "C:\my-site\env\lib\site-packages\translations\utils.py", line 199, in _get_purview
_fill_entity(entity, hierarchy)
File "C:\my-site\env\lib\site-packages\translations\utils.py", line 195, in _fill_entity
_fill_obj(obj)
File "C:\my-site\env\lib\site-packages\translations\utils.py", line 190, in _fill_obj
included=detail['included'],
File "C:\my-site\env\lib\site-packages\translations\utils.py", line 148, in _fill_entity
iterable, model = _get_entity_details(entity)
File "C:\my-site\env\lib\site-packages\translations\utils.py", line 132, in _get_entity_details
raise TypeError(error_message)
TypeError: `abc` is neither a model instance nor an iterable of model instances.
[log].ERROR Internal Server Error: /api/v2/learningproducts/
Traceback (most recent call last):
File "C:\my-site\env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\my-site\env\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\my-site\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\my-site\env\lib\site-packages\django\views\generic\base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "C:\my-site\env\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\my-site\env\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\my-site\env\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\my-site\env\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\my-site\env\lib\site-packages\rest_framework\generics.py", line 199, in get
return self.list(request, *args, **kwargs)
File "C:\my-site\env\lib\site-packages\rest_framework\mixins.py", line 40, in list
page = self.paginate_queryset(queryset)
File "C:\my-site\env\lib\site-packages\rest_framework\generics.py", line 171, in paginate_queryset
return self.paginator.paginate_queryset(queryset, self.request, view=self)
File "C:\my-site\env\lib\site-packages\rest_framework\pagination.py", line 216, in paginate_queryset
return list(self.page)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python36\lib\_collections_abc.py", line 883, in __iter__
v = self[i]
File "C:\my-site\env\lib\site-packages\django\core\paginator.py", line 188, in __getitem__
self.object_list = list(self.object_list)
File "C:\my-site\env\lib\site-packages\django\db\models\query.py", line 280, in __iter__
self._fetch_all()
File "C:\my-site\env\lib\site-packages\translations\querysets.py", line 54, in _fetch_all
with Context(self._result_cache, *self._trans_rels) \
File "C:\my-site\env\lib\site-packages\translations\context.py", line 21, in __init__
self.mapping, self.query = _get_purview(entity, hierarchy)
File "C:\my-site\env\lib\site-packages\translations\utils.py", line 199, in _get_purview
_fill_entity(entity, hierarchy)
File "C:\my-site\env\lib\site-packages\translations\utils.py", line 195, in _fill_entity
_fill_obj(obj)
File "C:\my-site\env\lib\site-packages\translations\utils.py", line 190, in _fill_obj
included=detail['included'],
File "C:\my-site\env\lib\site-packages\translations\utils.py", line 148, in _fill_entity
iterable, model = _get_entity_details(entity)
File "C:\my-site\env\lib\site-packages\translations\utils.py", line 132, in _get_entity_details
raise TypeError(error_message)
TypeError: `abc` is neither a model instance nor an iterable of model instances.
[basehttp].ERROR "GET /api/v2/learningproducts/ HTTP/1.1" 500 165340