Say I have a Comment Model like this:
class Comment(models.Model):
user = models.ForeignKey(User)
content = models.CharField(max_length=200)
create_time = models.DateTimeField(blank=True, default=datetime.datetime.now)
content_type = models.ForeignKey(ContentType, verbose_name=_('content type'))
object_id = models.PositiveIntegerField(_('object id'), db_index=True)
object = generic.GenericForeignKey('content_type', 'object_id')
If it's possible for me to select latest several comments for multiple objects in the same content_type (say Posts, giving ids of those Posts) in one query?
If that's possible, how about select the oldest comment and latest 4 comments for those objects all in one query?
Thx!
You mention "giving ids of those Posts", but don't actually have any Post model defined (in your question at least). But, as you're asking to get all comments with the same
content_type
, I'm not sure how the Posts are needed.The following query gets all
Comment
items that match aContentType
with a certain primary key (1), orders them bycreate_time
and selects the first 4 items: