A
A
Alexander2015-07-28 12:33:38
Django
Alexander, 2015-07-28 12:33:38

How to build a query in Django?

Hello, We
have the following models

class Category(models.Model):
  name = models.CharField(max_length=250)

class Item(models.Model):
  category = models.ForeignKey(Category)
  name = models.CharField(max_length=250)

class Price(models.Model):
  item = models.ForeignKey(Item)
  price = models.DecimalField(default=0, max_digits=7,decimal_places=2)
  update_time = models.DateTimeField(auto_now_add=True)

In fact, Price is a log of changes in the price of a product.
How to get a QuerySet using Django ORM to see the last time the Categories were updated. That is, you need something like:
Category ==> Update time (the maximum time of all price updates for products in this category)
Categories - about 20. Products - about 5000, prices - about 200,000.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
m0ody, 2015-07-28
@m0ody

Price.objects.order_by('item__category', '-update_time').distinct('item__category')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question