Is there a library for implementing ala Draper model decoration decorators in Django?

I want the "skin model decorators" to look like Draper for Rails in Django.

I already use get_absolute_url

variations of it (edit, delete, etc.), which makes it easier to access the generic URLs of the model, but now I find that there is so much written in templates:

{% if user has 'accounts.view_user' of article.author %}
    <a href="{{ article.author.get_absolute_url }}" class="fn">
        {{ article.author.name }}
    </a>
{% else %}
    {{ article.author.name }}
{% endif %}

      

Of course, it would be nice to have something more concise and dry, like:

{{ user.link }}

{{ user.edit_link }}

      

What Draper does is you can specify that the model passed to the template should be "decorated", ie. wrapped in a class that exposes additional methods, and optionally proxies anything that doesn't match the actual model class - separating the model-specific template logic away from data-oriented methods.

I really don't want to pollute my models in order to do this.

If I had to do something like this manually, I would like to do it like this:

def get_context_data(self, **kwargs):
    context = super(CapturesView, self).get_context_data(**kwargs)

    context.update({
        "article": ArticleDecorator(self.object)
    })

    return context

      

And ArticleDecorator

will define reference methods and use magic methods to pass any other attr calls to the real object Article

.

Doing it all by hand is pretty messy, however, and the Draper gem will automate things like auto-framing decorated model relationships, for example. ArticleDecorator.comments

will return a list CommentDecorator

, not Comments

.


Are there libraries to automate this kind of model abstraction for views?

+3


source to share


1 answer


In django, adding specific model behaviors without touching the original model is usually done with a proxy model



+1


source







All Articles