1
i wanted to know the difference between get_context_data and get_queryset.
And I would like to know your opinion about my codes.
What’s right and what’s wrong in them?
https://github.com/rg3915/vendas/blob/master/vendas_project/vendas/views.py#L121-L132
class SaleDetailView(DetailView):
template_name = 'vendas/sale/sale_detail.html'
model = Sale
def get_context_data(self, **kwargs):
s = Sale.objects.get(pk=self.kwargs['pk'])
sd = SaleDetail.objects.all().filter(sale=s)
context = super(SaleDetailView, self).get_context_data(**kwargs)
context['count'] = sd.count()
context['Sale'] = s
context['Itens'] = sd
return context
https://github.com/rg3915/vendas/blob/master/vendas_project/vendas/views.py#L74-L88
def get_queryset(self):
p = Product.objects.all()
q = self.request.GET.get('search_box')
# buscar por produto
if q is not None:
p = p.filter(product__icontains=q)
# filtra produtos em baixo estoque
if self.request.GET.get('filter_link', False):
p = p.filter(stock__lt=F('stock_min'))
return p
What can be improved in these codes?
Thanks for the feedback @mgibsonbr
– Regis Santos