这是我的模特课:
Google/Analytics pod
我想随机显示其中一个属性。这样做的最佳方式是什么?
答案 0 :(得分:0)
基于Django: Get list of model fields?和How to randomly select an item from a list?,您可能会写出类似的内容:
import random
class Player(models.Model):
name = models.CharField(max_length=50)
photo = models.ImageField()
games = models.IntegerField()
minutes = models.IntegerField()
points = models.IntegerField()
assists = models.IntegerField()
rebounds = models.IntegerField()
steals = models.IntegerField()
def random_field(self):
return random.choice([f.name for f in self._meta.get_fields()])