What's the correct way to use helpers to extend functionality in Django models?

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!

Maybe you did not show the full complexity for your methods, but from what you showed it could be simplified a bit.

You could use "direct/simple" inheritance, and if you have a lot of methods then for organization you could maybe prefix the method names with h1_ and h2_ to indicate which helper class they are from:

from django.db import models

class Helper1:
    def h1_hola(self):
        print("hola")

class Helper2:
    def h2_chau(self):
        print("chau")

class MyHelped(Helper1, Helper2, models.Model):
    pass

h = MyHelped()
h.h1_hola()
h.h2_chau()

Would this simpler approach be sufficient for your specific needs?

Back to Top