django-annotate

Django conditional Subquery aggregate

那年仲夏 提交于 2021-01-21 07:10:12
问题 An simplified example of my model structure would be class Corporation(models.Model): ... class Division(models.Model): corporation = models.ForeignKey(Corporation) class Department(models.Model): division = models.ForeignKey(Division) type = models.IntegerField() Now I want to display a table that display corporations where a column will contain the number of departments of a certain type, e.g. type=10 . Currently, this is implemented with a helper on the Corporation model that retrieves

Django conditional Subquery aggregate

余生颓废 提交于 2021-01-21 07:10:03
问题 An simplified example of my model structure would be class Corporation(models.Model): ... class Division(models.Model): corporation = models.ForeignKey(Corporation) class Department(models.Model): division = models.ForeignKey(Division) type = models.IntegerField() Now I want to display a table that display corporations where a column will contain the number of departments of a certain type, e.g. type=10 . Currently, this is implemented with a helper on the Corporation model that retrieves

GeoDjango: Distance Object is not serializable

安稳与你 提交于 2020-08-08 06:09:11
问题 I am just learning the geo-django. I can find out the distance of all places from a point. But when I use .values method to the annotated distance field, I am getting TypeError: Object of type 'Distance' is not JSON serializable Here is my code snippets #models.py import uuid from django.contrib.gis.db import models from django.contrib.gis.db.models.functions import Distance from django.contrib.gis.geos import Point class PlaceManager(models.GeoManager): def get_queryset(self): qs = super

How to annotate median value to django queryset

十年热恋 提交于 2020-06-29 03:56:26
问题 I have 2 models Puzzle and Play. for each play I have a rating. I would like to annotate to a Puzzle queryset the median rating value for all corresponding plays. class Puzzle(models.Model): name = models.CharField(max_length=255) class Play(models.Model): puzzle = models.ForeignKey(Puzzle, on_delete=models.CASCADE,related_name='plays') rating = models.IntegerField(default=-1) puzzle_completed = models.BooleanField(default=None, blank=False, null=False) I know how to count: Puzzle.objects

Django annotate with 'field' of 'related_name' of 'related_name'

℡╲_俬逩灬. 提交于 2020-04-30 06:33:09
问题 I have three models : Domain Topic Post Topic has a foreign key to Domain and Post has a foreign key to Topic . Post has a field updated_on . I want to annotate last_updated field in Domain queryset which would contain the latest post object from Post . Edit 1: Added models definition: class Board(models.Model): name = models.CharField(max_length=30, unique=True) description = models.CharField(max_length=50) creation_time = models.DateTimeField(auto_now_add=True) class Topic(models.Model):

Django - Annotate with count across ManytoMany relationships

白昼怎懂夜的黑 提交于 2020-01-25 09:31:08
问题 I am not able to find the way to annotate a queryset with a count of how many times an element is used in a many-to-many relationship. class Profile(models.Model): [...] # Profile can have multiple roles roles = models.ManyToManyField('Role', blank=True) [...] class Role(models.Model): company = models.ForeignKey(Company, on_delete=models.CASCADE) name = models.CharField(blank=True, max_length=30) description = models.CharField(blank=True, max_length=300) [...] For example I would have 5

Grouping queryset by date

依然范特西╮ 提交于 2019-12-24 08:26:03
问题 I have a class which contains a date as well as some other fields. What I'm trying to figure out is a query which will return me each of the items, grouped by the date. So, given the following class:- class Item(models.Model): item_date = models.DateField() <other fields> I'd like to get something like this:- [ { 'item_date': datetime.date(2018, 9, 12), 'items': <Queryset [<Item: item1>, <Item: item17> ...] }, { 'item_date': datetime.date(2018, 9, 13), 'items': <Queryset [<Item: item2>, <Item

Django 1.11 Annotating a Subquery Aggregate

允我心安 提交于 2019-12-17 08:22:07
问题 This is a bleeding-edge feature that I'm currently skewered upon and quickly bleeding out. I want to annotate a subquery-aggregate onto an existing queryset. Doing this before 1.11 either meant custom SQL or hammering the database. Here's the documentation for this, and the example from it: from django.db.models import OuterRef, Subquery, Sum comments = Comment.objects.filter(post=OuterRef('pk')).values('post') total_comments = comments.annotate(total=Sum('length')).values('total') Post

filter dataset not having a value

拈花ヽ惹草 提交于 2019-12-11 14:26:46
问题 Suppose i have a schema like below : Book | Author ------------- B1 | A1 B1 | A3 B1 | A2 B2 | A5 B2 | A4 B2 | A3 B3 | A5 B3 | A6 B3 | A1 B4 | A1 B4 | A5 B4 | A6 with below model: class Books(models.Model): author = models.CharField( max_length=100, blank=False, null=False, db_index=True, verbose_name=_('authors'), ) book = models.CharField( max_length=50, blank=False, null=False, db_index=True, verbose_name=_('book'), ) book_type = models.CharField( max_length=50, blank=False, null=False,

How to use annotate in query set to extract a count of type in a location?

◇◆丶佛笑我妖孽 提交于 2019-12-11 08:05:38
问题 I can't seem to properly use annotate to extract the information I need from my models. I have the follow .model structure: class ArtistWorkPlaceQuerySet(models.QuerySet): def with_related(self): return self.select_related('artist','work', 'place') class ArtistWorkPlaceManager(models.Manager): pass class PersonWorkPlace(models.Model): artist = models.ForeignKey(Artist, verbose_name=_('artist'), related_name='work', on_delete=models.CASCADE) work = models.ForeignKey(Work, verbose_name=_('work'