samedi 27 juillet 2019

how to refelct the variable used in making an instance from an object in its name?

I have this model in django:

class Level(Model):
    level = CharField(...)

I build some instances from this model:

[Level.objects.create(level='Grade {}'.format(i)) for i in range(1, 13)]

as a result, I have 12 instances. For example:

grade_11 = Level.objects.get(level='Grade 11')

What I want to do is that I build all grade_1 to grade_12 in one line. I know it is called introspection or reflection in python but I do not know how to it. I tried this but it was not successful:

['grade_{}'.format(i) = ProgramLevel.objects.get(program_level='Grade {}'.format(i)) for i in range(1, 13)]





Aucun commentaire:

Enregistrer un commentaire