Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
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!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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?

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!