Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda