think about these:
here is a function,
def calculate(model):
model.tempfield = 1
and this function will save a temp field in this model
and you can use model.tempfield in everywhere
but if it’s a queryset,after an order_by these temp filed will lost
how to order_by these temp field in queryset?
i have 2 model:
Class A(models.Model):
name = models.CharField(maxlength=100)
Class Log_Of_A(models.Model):
clicks = models.IntegerField()
a = models.ForeignKey(A)
date = models.DateField(db_index=True)
and calculate the Log of A by date
def createlog(request):
start = request.GET.get("start")
end = request.GET.get("end")
all_A = A.objects.all()
for a in all_A:
logs=Log_Of_A.objects.filter(a=a,date__gt=start,date__lt=end)
statistics = logs.aggregate(Sum("clicks"))
a.clicks = statistics["clicks__sum"]
all_A.order_by("clicks")
return all_A
how to order_by temporary field
Try this: