Link a function to a model entry

Viewed 77

I was looking at the result of this question on linking a specific method to a model entry, but not sure the solution used in the end is that elegant...

I want to have some sort of pointer to a specific method from a model entry in the database. This is my idea on how to do it but it seems abit clunky:

class Results(models.Model):
    result_ref = models.CharField(...)
    result_name = models.CharField(...)
    result_unit = models.CharField(...)

def result_1_method():
    return "My custom result for entry 1"

...

>>> results_map = {1: result_1_method, 2: result_2_method, ...}
>>> required_result = Results.objects.get(results_ref='XAY')
>>> required_result_method = results_map[required_result.id]
>>> result = required_result_method()

Is there a better or more commonly used solution? Like storing a method as part of the db entry for example? Thanks :)

1 Answers
Related