django-annotate

Multiple annotate with sum and display data in admin - Django

荒凉一梦 提交于 2019-12-05 02:15:14
问题 I'm new to both Django and Python. Currently I'm trying the Django Admin by doing. I've three models for a Django app, which are GoodsItem , SoldGoodsItem and FinishedGoodsItem . The models.py is: from django.db import models class GoodsItem(models.Model): name = models.CharField(max_length=255) size = models.DecimalField(max_digits=4, decimal_places=2) INCHES = 'IN' NUMBER = 'NUM' GOODS_ITEM_SIZE_UNITS = ( (INCHES, 'Inches'), (NUMBER, '#'), ) size_unit = models.CharField( max_length=4,

How to annotate a difference of datetime in days

☆樱花仙子☆ 提交于 2019-12-01 11:15:07
I have a Booking model that has start and end datetime fields. I want to know how many days a booking covers. I can do this in Python but I need this value for further annotations. Here's what I've tried: In [1]: Booking.objects.annotate(days=F('end')-F('start'))[0].days Out[1]: datetime.timedelta(16, 50400) There are a few problems here: I want an integer (or other number type I can use in calculations) of days as the output, not a timedelta. Setting output_field doesn't do anything meaningful here. My sums are based on date times . Subtractions like this, without removing the time could lead

Django annotate count in JSONField with Postgres

浪子不回头ぞ 提交于 2019-12-01 09:26:33
Using Django I have a field which is of type JSONField. I am wanting to get a distinct count on a nested key/value in the json. With a normal field you can just do soemthing like the following model.objects.values('field_name')\ .annotate(total=Count('field_name')).order_by('-total') This does not work for a JSONField. Example Model class Pet(models.Model): data = JSONField() Example of data { 'name':'sparky', 'animal':'dog', 'diet':{ 'breakfast':'biscuits', 'dinner':'meat', } } Trying Pet.objects.values('data__diet__dinner')\ .annotate(total=Count('data__diet__dinner')).order_by('-total')

Django annotate count in JSONField with Postgres

五迷三道 提交于 2019-12-01 04:21:26
问题 Using Django I have a field which is of type JSONField. I am wanting to get a distinct count on a nested key/value in the json. With a normal field you can just do soemthing like the following model.objects.values('field_name')\ .annotate(total=Count('field_name')).order_by('-total') This does not work for a JSONField. Example Model class Pet(models.Model): data = JSONField() Example of data { 'name':'sparky', 'animal':'dog', 'diet':{ 'breakfast':'biscuits', 'dinner':'meat', } } Trying Pet

Django queryset annotate field to be a list/queryset

北慕城南 提交于 2019-11-30 09:12:50
I'm trying to use django annotation to create queryset field which is a list of values of some related model attribute. queryset = ... qs = queryset.annotate( list_field=SomeAggregateFunction( Case(When(related_model__field="abc"), then="related_model__id") ), list_elements=Count(F('list_field')) ) I was thinking about about concatenating all these id with some separator, but i don't know the appropriate functions. Another solution is to make list_field a queryset . I know this syntax is wrong. Thank you for any help. If you are using postgresql and django >= 1.9 , you could use postgres

Django custom for complex Func (sql function)

爱⌒轻易说出口 提交于 2019-11-30 01:27:27
问题 In the process of finding a solution for Django ORM order by exact, I created a custom django Func: from django.db.models import Func class Position(Func): function = 'POSITION' template = "%(function)s(LOWER('%(substring)s') in LOWER(%(expressions)s))" template_sqlite = "instr(lower(%(expressions)s), lower('%(substring)s'))" def __init__(self, expression, substring): super(Position, self).__init__(expression, substring=substring) def as_sqlite(self, compiler, connection): return self.as_sql

Django queryset annotate field to be a list/queryset

徘徊边缘 提交于 2019-11-29 13:02:11
问题 I'm trying to use django annotation to create queryset field which is a list of values of some related model attribute. queryset = ... qs = queryset.annotate( list_field=SomeAggregateFunction( Case(When(related_model__field="abc"), then="related_model__id") ), list_elements=Count(F('list_field')) ) I was thinking about about concatenating all these id with some separator, but i don't know the appropriate functions. Another solution is to make list_field a queryset . I know this syntax is

django - annotate() - Sum() of a column with filter on another column

寵の児 提交于 2019-11-29 07:14:13
I have the following two models. class Product(models.Model): product_group=models.ForeignKey('productgroup.ProductGroup', null=False,blank=False) manufacturer=models.ForeignKey(Manufacturer, null=False,blank=False) opening_stock=models.PositiveIntegerField(default=0) class Meta: unique_together = ('product_group', 'manufacturer') and TRANSACTION_TYPE=(('I','Stock In'),('O','Stock Out')) class Stock(models.Model): product=models.ForeignKey('product.Product', blank=False,null=False) date=models.DateField(blank=False, null=False,) quantity=models.PositiveIntegerField(blank=False, null=False)

django - annotate() - Sum() of a column with filter on another column

爱⌒轻易说出口 提交于 2019-11-28 01:08:22
问题 I have the following two models. class Product(models.Model): product_group=models.ForeignKey('productgroup.ProductGroup', null=False,blank=False) manufacturer=models.ForeignKey(Manufacturer, null=False,blank=False) opening_stock=models.PositiveIntegerField(default=0) class Meta: unique_together = ('product_group', 'manufacturer') and TRANSACTION_TYPE=(('I','Stock In'),('O','Stock Out')) class Stock(models.Model): product=models.ForeignKey('product.Product', blank=False,null=False) date

Django 1.11 Annotating a Subquery Aggregate

巧了我就是萌 提交于 2019-11-27 03:12:25
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.objects.filter(length__gt=Subquery(total_comments)) They're annotating on the aggregate, which seems weird