Re: ListView from 2 model

2019-10-08 Thread Jani Tiainen
Hi,

ListView and most other generic views are designed mostly to work with
single model.

There are few ways to tackle your problem depending on your needs.

to 3. lokak. 2019 klo 0.53 Yann Mbella  kirjoitti:

> hi everyone,
> I got a problem in listing two models from a listview that is,  a list of
> one model and another model together (eg Books and Authors)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/915dbb5a-78bc-4d06-9a96-b0a43fa84be7%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAHn91ocSWw%2BMBaBcSfsGDquFQ0uqK9zr%3DN%2B%2B2TGMhaPmpKafRg%40mail.gmail.com.


Re: ListView from 2 model

2019-10-08 Thread lemme smash
you probably may want to implement `get_context_data` method like:
def get_context_data(self):
context = super(BlogView, self).get_context_data()
context.update({
'authors': 
Author.objects.filter(posts__isnull=False).distinct(),
'tags': Tag.objects.all(),
  })
return context



On Thursday, October 3, 2019 at 12:52:34 AM UTC+3, Yann Mbella wrote:
>
> hi everyone,
> I got a problem in listing two models from a listview that is,  a list of 
> one model and another model together (eg Books and Authors)
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/e19dcec9-258b-4411-b14b-d1152772be53%40googlegroups.com.


Re: ListView from 2 model

2019-10-07 Thread Pradeep Sukhwani
Hi Yann,

You can try queryset union functionality:

class MultiFilterPlacesListView(ListAPIView):
 """Custom queryset api view. Does not implement pagination"""

 pagination_class = None # Import custom pagination if you need the 
pagination.
 queryset = Places.objects.all()
 slice_size = 10  # count limit for each of the source queries

 def get_queryset(self):
 """Combine queries from new, editor choice and popular"""
 new_qs = self.queryset.filter(new_place=True)[:self.slice_size]
 editor_qs = self.queryset.filter(editor_choice=True)[:self.slice_size]
 popular_qs = self.queryset.filter(popular=True)[:self.slice_size]

 return new_qs.union(editor_qs, popular_qs, all=True) # Output will be 
union of the above two querysets


Doc: Django 

Example: StackOverflow 

Note:
if you are doing the union, then you won't get the pagination. In case, if 
you need pagination then you need to write custom pagination.



On Thursday, October 3, 2019 at 3:22:34 AM UTC+5:30, Yann Mbella wrote:
>
> hi everyone,
> I got a problem in listing two models from a listview that is,  a list of 
> one model and another model together (eg Books and Authors)
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/54a1b8ea-fbac-4a8c-a482-e172d88cafc9%40googlegroups.com.