I have 2 models
class Store(models.Model):
name = models.Charfield()
class Book(models.Model):
name = models.Charfield()
timestamp = models.Datetimefield()
store = models.ForeignKey(store)
What i currently do to get the latest book for each store is this:
stores = Store.objects.filter(name__contains = "stackoverflow")
latest_books = []
for store in stores:
latest_books.append(Book.objects.filter(store_id = store.id).latest("timestamp"))
However this can result in an insane amount of queries if there are too many stores
Is it possible to do this in one single query and how?
Im using django 1.5 and mysql, so the .distinct("timestamp") is not supported by mysql :)