[Answered ]-Django, model instance attribute which is not a field?

2👍

You can set the attribute of an object any time your want:

import random

objects = []
for obj in MyModel.objects.all():
    obj.score = random.randint(0, 100)
    objects.append(obj)

sorted_objects = sorted(objects, key=lambda x: x.score)

0👍

import random
sorted_objects = sorted(map(lambda x: x.score = random.randint(0,100),MyModel.objects.all()),key = lambda x:x.score)

Leave a comment