Answer the question
In order to leave comments, you need to log in
Fetching data from the database. QuerySet?
Hello!
The crux of the matter is as follows. I'm trying to write a simple application to collect the number of workers on objects. There are
models
class SObjects(models.Model):
title = models.CharField(max_lenght=128)
class Subpodr(models.Model):
sobj = models.ForeignKey(SObjects)
name = models.CharField(max_lenght=128)
class Peoples(models.Model):
sobj = models.ForeignKey(SObjects)
subpodr = ForeignKey(Subpodr)
date = models.DateField(default=datetime.datetime.today())
rab = models.IntegerField(default = 0)
def calc(sobj, date):
return Peoples.objects.filter(date=date, sobj=sobj).aggregate(Sum('rab'))
Answer the question
In order to leave comments, you need to log in
If I remember correctly:
class Peoples(models.Model):
subpodr = ForeignKey(Subpodr)
date = models.DateField(default=datetime.datetime.today())
rab = models.IntegerField(default = 0)
class Meta:
unique_together = ('subpodr', 'date')
peoples_for_main_page = Peoples.objects.values('subpodr__sobj', 'date', 'rab').annotate(
num_peoples=Sum('rab')).order_by('date', 'subpodr__sobj')
sobject = Sobject.objects.get(pk=current_sobject_pk)
peoples_for_sobject_page = Peoples.objects.filter(subpodr__sobj=sobject).annotate(
num_peoples=Sum('rab')).order_by('date', 'subpodr')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question