I have two models like this:
class ItemType(models.Model):
type = models.CharField(max_length=100)
class Items(models.Model):
item_type = models.ForeignKey(ItemType, on_delete=models.CASCADE)
item_amount = models.FloatField()
now I want to get sum of item_amount according to item_type. How I can do this?
Try something like this.
from django.db.models import Sum
result = Items.objects.values('item_type')
.order_by('item_type')
.annotate(total_amount=Sum('item_amount'))
Related
these are my models
class Countries(models.Model):
Name = models.CharField(max_length=100)
Language = models.IntegerField()
Population = models.IntegerField(default=0)
class World(models.Model):
Languages_spoken = model.Charfield(max_length=12000)
World_population = models.IntegerField(default=0)
I am trying to add Population of all instances in Countries to sum and show on World_population field on class World
What I have tried
class World(models.Model):
Languages_spoken = model.Charfield(max_length=12000)
World_population = models.IntegerField(default=0)
def save(self, *args, **kwargs):
self.World_population = Countries.objects.get(Population) # I know this is not correct
super(World,self).save()
Give this a shot:
from django.db.models import Sum
Countries.objects.aggregate(total_population=Sum('Population'))
More info on aggregation here
You can use Sum() here,
self.World_population = Countries.objects.aggregate(Sum('Population'))
https://docs.djangoproject.com/en/2.1/topics/db/aggregation/
I have two databases, A and B.
B contains a ForeignKey to A.
When I do B.objects.filter(a_id=3).values('bags').count(), I get the number I want, Y.
What is the set of commands I need in order to add this number, Y, as an annotation into database A?
Ideally, this would be an annotate type of command.
The models look like:
class A(models.Model):
name = models.CharField(max_length=150)
class B(models.Model):
name = models.CharField(max_length=150)
a_id = models.ForeignKey(A, on_delete=models.CASCADE)
bags = models.ManyToManyField(Bags)
class Bags(models.Model):
name = models.CharField(max_length=150)
Try to use b__bags lookup in annotation:
from django.db.models import Count
A.objects.annotate(bags_count=Count('b__bags'))
from django.db.models import Count
A.objects.annotate(Y=Count('b__bags'))
I have such models
class Category(models.Model):
name = models.CharField()
…
class Product(models.Model):
category = models.ForeignKey(Category, verbose_name=‘cat’)
name = models.CharField()
price = models.IntegerField()
I need to create queryset which will select all products with price >= 100 grouped by categories. Afterwards I need to get count of products in each category.
I did
categories = Category.objects.filter(product__price__gte = 100)
This queryset gave me all categories which contain product with price >= 100. But how can I get count of products in it and products themseves? Maybe I need to use a prefetch_related, but I don't know how.
You can achieve this usingCount with conditional aggregation from django.
categories = Category.objects.filter(product__price__gte = 100).\
annotate(count_of_products=Count('products', filter=Q(product__price__gte = 100)))
let's say I have 2 models:
class Recipe(models.Model):
recipe = models.TextField()
ingredients = models.ManyToManyField(Ingredient)
class Ingredient(models.Model):
name = models.CharField()
and I want to know what ingredient is the most used in all the recipes.
How do I query that?
Read about aggregation and annotations at https://docs.djangoproject.com/en/dev/topics/db/aggregation/
To get the name of the most common ingredient:
from django.db.models import Count
most_common = Ingredient.objects.annotate(num_recipes=Count('recipe')).order_by('-num_recipes')[0]
print most_common.name
class Order(models.Model):
...
class OrderItem(models.Model)
order = models.ForeignKey(Order)
product = models.ForeignKey(Product)
quantity = models.PositiveIntegerField()
What I need to do is to get the Order(s) which has only one order item. How can I write it using the QuerySet objects (without writing SQL)?
The easiest way to do this would be to use the Count aggregation:
from django.db.models import Count
Order.objects.annotate(count = Count('orderitem__id')).filter(count = 1)