jeudi 30 avril 2020

Is there an equivalent of the exhibit pattern in Python/Django?

I have tried googling it but I get flooded with results on wrappers when trying to query decorators.

In the Rails world, I used to use https://github.com/objects-on-rails/display-case, " An implementation of the Exhibit pattern, as described in Objects on Rails" a lot, in essence:

class LeagueExhibit < DisplayCase::Exhibit
  def self.applicable_to?(object, context)
    object.class.name == 'League'
  end

  def render_icon(template)
    # Do something
  end
end

and then wrap with something like @leagues = exhibit(League.all) which then will allow @leagues.render_icon or @leagues.a_league_method.

I want to do the same with a Django view, I have some helper methods for one of my models that are needed only in that view. Ideally I would be interested in passing the target object to a class and when a method does not exist on that class, to delegate the call to that object.

Does this exist in Python? Is there a library or is it so trivial that can be implemented with a dunder method?

Aucun commentaire:

Enregistrer un commentaire