简体   繁体   中英

Combine two Django queries in a certain order

I have two django queries in my view. They are as follows:

news = News.objects.all()[:8]
posts = Post.objects.all()[:3]

I need to combine these two queries together in the following order:

[news, post, news, news, news, post, news, post, news, news, news]

I am pretty sure that I would have to use lambda or do some sort of count , but I am a little lost at this point. Should I just be using count and append ?

No.

def selector(seqs, picks):
  iters = [iter(x) for x in seqs]
  for choice in picks:
    yield next(iters[choice])

print list(selector((news, post), (0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM