django - Generating a single queryset with filtered summary data across a foreign key? -
i have small django project learn (it's web ui rancid backup software) , i've run problem.
the model app defines devices, , devicegroups. each device member of group , has couple of state flags - enabled, successful - indicate if operating correctly. here's relevant bits.
class devicegroup(models.model): group_name = models.charfield(max_length=60,unique=true) class device(models.model): hostname = models.charfield(max_length=60,unique=true) enabled = models.booleanfield(default=true) device_group = models.foreignkey(devicegroup) last_was_success = models.booleanfield(default=false,editable=false)
i have summary table on front 'dashboard' page, shows list of groups, , each group, how many devices in it. i'd show number of active devices, , number of failing (i.e. not last_was_success) devices per-group. plain device count available through foreignkey field.
this seems kind of thing annotate for, not quite. , actually, i'm not sure how i'd raw sql either. 3 queries , lookup afterwards, or subqueries.
so - possible 'nicely' in django? or alternatively, how do joining again in template or view? object passed template simply:
device_groups = devicegroup.objects.order_by('group_name')
currently, , don't think can add fields onto queryset results "manually", can i? i.e. it's not dict or similar.
i think must use
device_groups = devicegroup.objects.all().order_by('group_name')
or
device_groups = devicegroup.objects.filter(condition).order_by('group_name')
Comments
Post a Comment