django filter queryset based on count of another queryset
Date : March 29 2020, 07:55 AM
will be helpful for those in need First, you can query list of book which has review, and then get compliment by using django exclude bookname_has_review = Review.objects.all().distinct().values_list('bookname', flat=True)
book_not_have_review = Book.objects.all().exclude(name__in=bookname_has_review)
|
How to create Prefetch queryset based on parent queryset in django
Tag : django , By : Michael Gunderson
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further Here is the scenario, a project model which contains multiple bids. Project.objects.filter(whatever condition).prefetch_related(
Prefetch('bids', queryset=Bid.objects.all())
)
Project.objects.filter(whatever condition).prefetch_related('bids')
Project.objects.filter(whatever condition).prefetch_related(
Prefetch('winning_bids', queryset=Bid.objects.filter(status='WINNING'))
)
|
How to display a template based on a queryset done in a class based view where one of the argument for queryset comes fr
Date : March 29 2020, 07:55 AM
around this issue Your view should be based on Post. Then you can use all the existing logic of the view but additionally filter by the author username. class AuthorPostIndexView(ListView):
model = Post
template_name ='authorpostindex.html'
def get_queryset(self):
queryset = super().get_queryset()
username = self.kwargs['username']
return queryset.filter(authors__username=username)
|
Filter the queryset based on the domain making the request
Date : March 29 2020, 07:55 AM
Any of those help The qet_queryset [drf-doc] is supposed to, like the name suggests, return a QuerySet, not the response of that queryset. You should simply return: class ListProperties(generics.ListAPIView):
queryset = models.Property.objects.all()
serializer_class = frontend.PropertyCard
filter_class = filters.PropertyFilterset
pagination_class = pagination.PropertyPageNumberPagination
def get_queryset(self):
domain = self.request.META['HTTP_DOMAIN']
return self.queryset.filter(company__domain=domain)
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response(serializer.data)
|
ListView is missing a QuerySet. Define ListView.model, ListView.queryset, or override ListView.get_queryset()
Tag : python , By : Mossy Breen
Date : March 29 2020, 07:55 AM
Any of those help There is an error in your urls.py, you did not refer to the SchoolListView, but to the generic ListView itself. You can fix this by writing: # app/urls.py
from django.urls import path
from . import views
#My name space
app_name = 'basicapp'
urlpatterns = [
# SchoolListView instead of ListView
path('', views.SchoolListListView.as_view(), name='list'),
# probably SchoolDetailView instead of DetailView, and with a pk in the url
path('details', views.DetailView.as_view(), name='details')
]
|