[Answered ]-Select random field from database in Python

1👍

You can use random.choice like this:

import random

columns = ['word1', 'word2', 'word3']
chosen_column = random.choice(columns)

#now access DB using chosen_column for example:
Your_model.objects.raw('SELECT ' + chosen_column + ' FROM myapp_yourmodel')
👤Gamopo

Leave a comment