Django FieldError : Cannot resolve keyword 'total_sales' into field
Django FieldError : Cannot resolve keyword 'total_sales' into field
这是我的查询 运行 获取每一方的总销售额。
Party.objects.annotate(total_sales=Sum('sales__salestransaction__total_cost'))
它显示正确的结果。但是当我尝试在我的视图中应用 get_queryset 时,它不起作用并显示一个 FieldError 是:
无法将关键字 'total_sales' 解析为字段。选项有:party_address、party_id、party_name、party_phone、销售
我的看法
class PartyListView(ListView):
paginate_by = 2
model = Party
template_name = 'mael/parties.html'
def querystring(self):
qs = self.request.GET.copy()
qs.pop(self.page_kwarg, None)
return qs.urlencode()
def get_queryset(self):
qs = super().get_queryset()
if 'q' in self.request.GET:
search_txt = self.request.GET['q']
qs = qs.filter(party_name__icontains=search_txt).annotate(total_sales=Sum('sales__salestransaction__total_cost'))
return qs.order_by('total_sales')
def get(self, request):
form = PartyForm()
party_list = self.get_queryset()
qrstring = self.querystring()
paginator = Paginator(party_list, 5)
page_number = request.GET.get('page')
party_list = paginator.get_page(page_number)
return render(request, self.template_name, {'form': form, 'party_list': party_list, 'querystring': qrstring})
型号
class Party(models.Model):
party_id = models.BigAutoField(primary_key=True)
party_name = models.CharField(max_length=128)
party_phone = models.CharField(max_length=128)
party_address = models.CharField(max_length=128)
def __str__(self):
return self.party_name
class Sales(models.Model):
invoice_no = models.BigAutoField(primary_key=True)
invoice_date = models.DateField(default=date.today)
party = models.ForeignKey(Party, on_delete=models.CASCADE)
def __str__(self):
return str(self.invoice_no)
class SalesTransaction(models.Model):
sales = models.ForeignKey(Sales, on_delete=models.CASCADE)
item_qty = models.PositiveIntegerField(default=0)
total_cost = models.PositiveIntegerField(default=0)
def __str__(self):
return self.item_name
get_queryset 函数有什么问题,我该如何解决这个错误?请帮忙
如果if 'q' in self.request.GET
returns False
你不能.order_by('total_sales')
,因此你应该在这两种情况下注释:
def get_queryset(self):
qs = super().get_queryset()<strong>.annotate(</strong>
total_sales=Sum('sales__salestransaction__total_cost')
<strong>)</strong>
if 'q' in self.request.GET:
search_txt = self.request.GET['q']
qs = qs.filter(party_name__icontains=search_txt)
return qs.order_by('total_sales')
这是我的查询 运行 获取每一方的总销售额。
Party.objects.annotate(total_sales=Sum('sales__salestransaction__total_cost'))
它显示正确的结果。但是当我尝试在我的视图中应用 get_queryset 时,它不起作用并显示一个 FieldError 是:
无法将关键字 'total_sales' 解析为字段。选项有:party_address、party_id、party_name、party_phone、销售
我的看法
class PartyListView(ListView):
paginate_by = 2
model = Party
template_name = 'mael/parties.html'
def querystring(self):
qs = self.request.GET.copy()
qs.pop(self.page_kwarg, None)
return qs.urlencode()
def get_queryset(self):
qs = super().get_queryset()
if 'q' in self.request.GET:
search_txt = self.request.GET['q']
qs = qs.filter(party_name__icontains=search_txt).annotate(total_sales=Sum('sales__salestransaction__total_cost'))
return qs.order_by('total_sales')
def get(self, request):
form = PartyForm()
party_list = self.get_queryset()
qrstring = self.querystring()
paginator = Paginator(party_list, 5)
page_number = request.GET.get('page')
party_list = paginator.get_page(page_number)
return render(request, self.template_name, {'form': form, 'party_list': party_list, 'querystring': qrstring})
型号
class Party(models.Model):
party_id = models.BigAutoField(primary_key=True)
party_name = models.CharField(max_length=128)
party_phone = models.CharField(max_length=128)
party_address = models.CharField(max_length=128)
def __str__(self):
return self.party_name
class Sales(models.Model):
invoice_no = models.BigAutoField(primary_key=True)
invoice_date = models.DateField(default=date.today)
party = models.ForeignKey(Party, on_delete=models.CASCADE)
def __str__(self):
return str(self.invoice_no)
class SalesTransaction(models.Model):
sales = models.ForeignKey(Sales, on_delete=models.CASCADE)
item_qty = models.PositiveIntegerField(default=0)
total_cost = models.PositiveIntegerField(default=0)
def __str__(self):
return self.item_name
get_queryset 函数有什么问题,我该如何解决这个错误?请帮忙
如果if 'q' in self.request.GET
returns False
你不能.order_by('total_sales')
,因此你应该在这两种情况下注释:
def get_queryset(self):
qs = super().get_queryset()<strong>.annotate(</strong>
total_sales=Sum('sales__salestransaction__total_cost')
<strong>)</strong>
if 'q' in self.request.GET:
search_txt = self.request.GET['q']
qs = qs.filter(party_name__icontains=search_txt)
return qs.order_by('total_sales')