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)
Related
please I need your help how to reduce the database call when using ModelChoiceField as it requires a queryset and I have to use it three times separately with a model that is recursively foreign key on itself, the code is below:
ModelForm code in the init function
self.fields['category'] = forms.ModelChoiceField(queryset=queryset)
self.fields['super_category'] = forms.ModelChoiceField(queryset=)
self.fields['product_type'] = forms.ModelChoiceField(queryset=)
the model class:
class Category(ProjectBaseModel, AuditLogMixin):
parent_id = models.ForeignKey('self', related_name='children', blank=True, null=True, on_delete=models.CASCADE,verbose_name=_('Parent'))
what i tried to do is collect all ids of the desired categories in array and make only one filter queryset with them like the following:
category = auction.category
super_category = category.parent_id
product_type = super_category.parent_id
ids= [category.id,super_category.id,product_type.id]
queryset = Category.objects.filter(id__in=ids)
How to proceed with that solution
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'))
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