class Screening(models.Model):
def get_omitted_1(self):
return OmittedInformationReason.objects.get(pk=self.omitted_1).name
get_omitted_1 = property(get_omitted_1)
I want to have access to screening_instance.get_omitted_1 ,screening_instance.get_omitted_2 upto screening_instance.get_omitted_10. Is there better way to do this without creating get_omitted_1, get_omitted_2.. methods for each case like get_omitted_%d ??
I tried creating dynamic function at runtime but had some problem with it. I tried something like this:
def omitted_list(self,x):
def omitted_function(self):
opk = "omitted_%d" % x
return OmittedInformationReason.objects.get(pk=self.opk).name
omitted_name = 'get_omitted_'+ str(x)
setattr(self.__class__, omitted_name , omitted_function)
omitted_name = property(omitted_name)
for x in range(1, 3):
omitted_list(x)