0

Have a look at the following code snippet:

def post_proposals(count):
    post_elements = chain(
            PostModel1.objects.all(),
            PostModel2.objects.all(),
            PostModel3.objects.all(),
        )
    post_elements_list = list(post_elements)
    post_proposals = random.sample(post_elements_list, count)
    return post_proposals

Where it's requiered to generate a list of proposals from 3 different Post tables, how to optimize a disgn flaw like this? Already came accorss the idea to build a Index table where all PKs of all my PostModel{1..3} are stored, so that I just have to query 1 table instead of three. Does smb. have a practical idea on how to solve this or at least optimizing it?

  • one possible solution is that you can decompose `count` into three small integers, then randomly get these number of rows accordingly using `Model.objects.order_by('?')[:n]` as mentioned here https://stackoverflow.com/questions/1731346/how-to-get-two-random-records-with-django – minglyu Jun 01 '20 at 21:11
  • Thanks for that comment, will have a look at it :D –  Jun 02 '20 at 22:56

0 Answers0