Estas son las clases:
Código Python:
Ver original
class ListPosts(ListView): '''Blogs posts''' context_object_name = 'posts' template_name = 'index/index.html' paginate_by = 3 def get_queryset(self): lang_pk = Language.objects.filter(locale=self.request.LANGUAGE_CODE) posts = Post.objects.filter(status='p') posts_translated = PostTranslation.objects.filter( post=posts, language=lang_pk ) return posts_translated class ListWorks(ListView): '''Portfolio works''' context_object_name = 'works' template_name = 'index/index.html' paginate_by = 3 def get_queryset(self): lang_pk = Language.objects.filter(locale=self.request.LANGUAGE_CODE) works = Work.objects.filter(status='p') works_translated = WorkTranslation.objects.filter( work=works, language=lang_pk ) return works_translated
He probado a crear una tercera clase que retorne ambas querysets, pero no he logrado que funcione. Una de las formas que probé es esta:
Código Python:
Ver original
class ListAll(TemplateView, ListPosts, ListWorks): template_name = 'index/index.html' def get_context_data(self, **kwargs): context = super(ListAll, self).get_context_data(**kwargs) posts = ListPosts.get_queryset() works = ListWorks.get_queryset() if posts: context['posts'] = posts if works: context['works'] = works return context
En este caso, la aplicación me da el siguiente error:
Código:
Mediante una única clase me funciona correctamente, pero me gustaría saber como hacerlo funcionar con clases independientes por simple curiosidad'ListAll' object has no attribute 'object_list'
De esta forma es como me funciona:
Código Python:
Ver original
class ListAll(TemplateView): template_name = 'index/index.html' def get_context_data(self, **kwargs): context = super(ListAll, self).get_context_data(**kwargs) lang_pk = Language.objects.filter(locale=self.request.LANGUAGE_CODE) posts = Post.objects.filter(status='p') works = Work.objects.filter(status='p') context['posts'] = PostTranslation.objects.filter( post=posts, language=lang_pk )[:3] context['works'] = WorkTranslation.objects.filter( work=works, language=lang_pk )[:3] return context
Saludos