I'm adding some extended functionallity to my models in Django. In order to not overload the model root interface (and file), i'm using some helpers as attributes of the model.
What i want is to group the methods and properties into this helpers, and what i want is something like:
class Product(models.Model):
downloads = DownloadsHelper()
# ....
pass
p = Product.objects.first()
p.downloads.files_count(p)
p.downloads.reset_permissions(p)
# ...
In order to not to have to pass the instance to the helper each time, I could use another approach.
class Product(models.Model):
def __init__(self, *args, **kwargs):
super(Product, self).__init__(*args, **kwargs)
self.downloads = DownloadsHelper(self)
self.shipping = ShippingHelper(self)
p = Product.objects.first()
p.downloads.files_count
p.downloads.reset_permissions()
And finally, a more python-generic/conceptual way to do this stuff would be like:
class Helper:
def __init__(self, helped):
self.helped = helped
class Helper1(Helper):
attribute_name = 'helloing'
def hola(self): print("hola")
class Helper2(Helper):
attribute_name = 'goodbying'
def chau(self): print("chau")
class Helped:
def __init__(self):
self._install_helpers()
def _install_helpers(self):
for helper in self.helpers:
setattr(self, helper.attribute_name, helper(self))
class MyHelped(Helped):
helpers = [Helper1, Helper2]
h = MyHelped()
h.helloing.hola()
h.goodbying.chau()
And the question is: Is this last approach a correct way /good practice to do the stuff from a pythonic-OOP and "Djangoid" point of view. Has this any problem?
Thanks for reading!