Python 如何使用基于类的视图继承来重写父类?
在我的ShowChart中有一个名为Electronic(Electronic.objects.values..etc)的模型,在我的继承类(ChartElectrical)中,它需要更改为Electronic(Electronic.objects.values..etc),这里我只传递它。我不知道怎么做Python 如何使用基于类的视图继承来重写父类?,python,django,django-class-based-views,Python,Django,Django Class Based Views,在我的ShowChart中有一个名为Electronic(Electronic.objects.values..etc)的模型,在我的继承类(ChartElectrical)中,它需要更改为Electronic(Electronic.objects.values..etc),这里我只传递它。我不知道怎么做 class ShowChart(View): def get(self, request): my_count = Electrical.objects.values('all
class ShowChart(View):
def get(self, request):
my_count = Electrical.objects.values('allocated_time')\
.annotate(complete=Count('allocated_time', filter=Q(batch_18=True)),
not_complete=Count('allocated_time',
filter=Q(batch_18=False)),
complete_1=Count('allocated_time',
filter=Q(batch_19=True)),
not_complete_1=Count('allocated_time',
filter=Q(batch_19=False)),
complete_2=Count('allocated_time',
filter=Q(batch_20=True)),
not_complete_2=Count('allocated_time',
filter=Q(batch_20=False)),
complete_3=Count('allocated_time',
filter=Q(batch_21=True)),
not_complete_3=Count('allocated_time',
filter=Q(batch_21=False)))
c_batch_18 = list()
n_batch_18 = list()
c_batch_19 = list()
n_batch_19 = list()
c_batch_20 = list()
n_batch_20 = list()
c_batch_21 = list()
n_batch_21 = list()
for entry in my_count:
c_batch_18.append(entry['complete'] * entry['allocated_time'])
n_batch_18.append(entry['not_complete'] * entry['allocated_time'])
c_batch_19.append(entry['complete_1'] * entry['allocated_time'])
n_batch_19.append(entry['not_complete_1'] * entry['allocated_time'])
c_batch_20.append(entry['complete_2'] * entry['allocated_time'])
n_batch_20.append(entry['not_complete_2'] * entry['allocated_time'])
c_batch_21.append(entry['complete_3'] * entry['allocated_time'])
n_batch_21.append(entry['not_complete_3'] * entry['allocated_time'])
survived_series = [sum(c_batch_18), sum(c_batch_19), sum(c_batch_20), sum(c_batch_21), 0]
not_survived_series = [sum(n_batch_18), sum(n_batch_19), sum(n_batch_20), sum(n_batch_21), 0]
return render(request, 'chart.html', {'survived_series': json.dumps(survived_series),
'not_survived_series': json.dumps(not_survived_series)})
class ChartElectrical(ShowChart):
pass
我认为应该用另一种方法移动视图的
my_count
部分,然后在子视图中覆盖它。像这样:
class ShowChart(View):
def get_my_count(self):
my_count = Electrical.objects.values('allocated_time')\
.annotate(complete=Count('allocated_time', filter=Q(batch_18=True)),
not_complete=Count('allocated_time',
filter=Q(batch_18=False)),
complete_1=Count('allocated_time',
filter=Q(batch_19=True)),
not_complete_1=Count('allocated_time',
filter=Q(batch_19=False)),
complete_2=Count('allocated_time',
filter=Q(batch_20=True)),
not_complete_2=Count('allocated_time',
filter=Q(batch_20=False)),
complete_3=Count('allocated_time',
filter=Q(batch_21=True)),
not_complete_3=Count('allocated_time',
filter=Q(batch_21=False)))
return my_count
def get(self, request):
c_batch_18 = list()
n_batch_18 = list()
c_batch_19 = list()
n_batch_19 = list()
c_batch_20 = list()
n_batch_20 = list()
c_batch_21 = list()
n_batch_21 = list()
for entry in self.get_my_count():
c_batch_18.append(entry['complete'] * entry['allocated_time'])
n_batch_18.append(entry['not_complete'] * entry['allocated_time'])
c_batch_19.append(entry['complete_1'] * entry['allocated_time'])
n_batch_19.append(entry['not_complete_1'] * entry['allocated_time'])
c_batch_20.append(entry['complete_2'] * entry['allocated_time'])
n_batch_20.append(entry['not_complete_2'] * entry['allocated_time'])
c_batch_21.append(entry['complete_3'] * entry['allocated_time'])
n_batch_21.append(entry['not_complete_3'] * entry['allocated_time'])
survived_series = [sum(c_batch_18), sum(c_batch_19), sum(c_batch_20), sum(c_batch_21), 0]
not_survived_series = [sum(n_batch_18), sum(n_batch_19), sum(n_batch_20), sum(n_batch_21), 0]
return render(request, 'chart.html', {'survived_series': json.dumps(survived_series),
'not_survived_series': json.dumps(not_survived_series)})
class ChartElectrical(ShowChart):
def get_my_count(self):
# overriding get_my_count function
my_count = Electrical.objects.values(...)
return my_count
可能的优化
此外,以下是一些优化想法:
1.您可以通过注释进行c\u batch\u 18
,n\u batch\u 18
等计算。像这样:
class ShowChart(View):
def get_my_count(self):
my_count = Electrical.objects.values('allocated_time')\
.annotate(complete=Count('allocated_time', filter=Q(batch_18=True)),
not_complete=Count('allocated_time',
filter=Q(batch_18=False)),
complete_1=Count('allocated_time',
filter=Q(batch_19=True)),
not_complete_1=Count('allocated_time',
filter=Q(batch_19=False)),
complete_2=Count('allocated_time',
filter=Q(batch_20=True)),
not_complete_2=Count('allocated_time',
filter=Q(batch_20=False)),
complete_3=Count('allocated_time',
filter=Q(batch_21=True)),
not_complete_3=Count('allocated_time',
filter=Q(batch_21=False)))
return my_count
def get(self, request):
c_batch_18 = list()
n_batch_18 = list()
c_batch_19 = list()
n_batch_19 = list()
c_batch_20 = list()
n_batch_20 = list()
c_batch_21 = list()
n_batch_21 = list()
for entry in self.get_my_count():
c_batch_18.append(entry['complete'] * entry['allocated_time'])
n_batch_18.append(entry['not_complete'] * entry['allocated_time'])
c_batch_19.append(entry['complete_1'] * entry['allocated_time'])
n_batch_19.append(entry['not_complete_1'] * entry['allocated_time'])
c_batch_20.append(entry['complete_2'] * entry['allocated_time'])
n_batch_20.append(entry['not_complete_2'] * entry['allocated_time'])
c_batch_21.append(entry['complete_3'] * entry['allocated_time'])
n_batch_21.append(entry['not_complete_3'] * entry['allocated_time'])
survived_series = [sum(c_batch_18), sum(c_batch_19), sum(c_batch_20), sum(c_batch_21), 0]
not_survived_series = [sum(n_batch_18), sum(n_batch_19), sum(n_batch_20), sum(n_batch_21), 0]
return render(request, 'chart.html', {'survived_series': json.dumps(survived_series),
'not_survived_series': json.dumps(not_survived_series)})
class ChartElectrical(ShowChart):
def get_my_count(self):
# overriding get_my_count function
my_count = Electrical.objects.values(...)
return my_count
从django.db.models导入ExpressionWrapper,IntegerField
mycount=mycount.annotate(c_batch_18=ExpressionWrapper(
F('complete')*F('allocated_time'),output_field=IntegerField())
.annotate(n_batch_18=ExpressionWrapper(
F('not_complete')*F('allocated_time')、output_field=IntegerField())#等等
2.您还可以通过聚合计算和:
从django.db.models导入总和
my_count_sums=my_count.aggregate(c_batch_18_sum=sum('c_batch_18')、n_batch_18=sum('n_batch_18'))等等
生存=列表(过滤器(lambda x:x.key().startswith('c_batch'),my_count_sums))
not_surved=list(过滤器(lambda x:x.key().startswith('n_batch'),my_count_sums))
通过使用这些优化,您将通过DB完成大部分计算,而不是依赖python资源。是的,它正在工作!非常感谢您的支持!很好,请考虑把它标记为。此外,请检查优化的想法,与此答案共享