Skip to content

Instantly share code, notes, and snippets.

@edgabaldi
Last active September 3, 2015 20:05
Show Gist options
  • Select an option

  • Save edgabaldi/842a01be584a87065fce to your computer and use it in GitHub Desktop.

Select an option

Save edgabaldi/842a01be584a87065fce to your computer and use it in GitHub Desktop.
Annotate example
class Company(models.Model):
name = models.CharField(max_length=100)
class Product(models.Model):
name = models.CharField(max_length=100)
class Quotation(models.Model):
product = models.ForeignKey('app.Product', related_name=quotations)
company = models.ForeignKey('app.Company')
price = models.DecimalField(...)
queryset = Product.objects.annotate(min_value=Min('quotations__price')).values('name', 'quotations__company', 'min_value')
for product in queryset:
print product['name'], product['quotations__company'], product['min_value']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment