On 08/09/2013 08:17 μμ, Yegor Roganov wrote:
I'm developing a forum and my db schema  is roughly equivalent to the
following:
class Topic(models.Model):
     head = models.CharField()
     body = models.TextField()
     created = models.DateTimeField(auto_now_add=True)

class Post(models.Model):
     body = models.TextField()
     topic = models.ForeignKey(Topic)
         created = models.DateTimeField(auto_now_add=True)

The question is: how to construct a query to fetch 20 latest Topics with 5
latest Posts for each of these Topics ? As I understood, prefetch_related
isn't suitable since it cannot limit the number of related objects.

It looks like you are looking for a subquery

maybe this will be useful

http://stackoverflow.com/questions/8217490/query-of-a-subquery-in-django

--
 --------------------------------------------------------------
 Nick Apostolakis
 Msc in IT, University of Glasgow
 e-mail: nicka...@oncrete.gr
 Web Site: http://nick.oncrete.gr
 --------------------------------------------------------------

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to