Django编码风格:我应该在这里使用类装饰器吗?

时间:2012-04-14 20:37:48

标签: python django coding-style decorator django-generic-views

我对这整个Django事情都很陌生,因此,我决定从基于ClassBased的GenericViews开始,而不是基于较旧的基于Generics的功能,这导致了一些绊脚石缺乏文档。无论如何,到目前为止,我真的很喜欢GenericViews并且已经使用装饰器扩展它们。

我一直在做的简单例子

ideapad/urls.py

from ideapad.views import *

urlpatterns = patterns('ideapad.views',
        url(r'^$', IndexListView.as_view(), name='url_index'),
        )

ideapad/views.py

from django.views.generic import ListView
from ideapad.models import Idea
from sauce.decorators import sort_queryset

@sort_queryset('-date')
class IndexListView(ListView):
    model = Idea
    template_name = 'ideapad/index.html'
    context_object_name = 'ideas'

sauce/decorators.py

def sort_queryset(default_sort):
    """
    Decorator that sorts queryset according to GET data
    Made to wrap GenericViews with get_queryset methods
    """
    def decorator(cls):
        fn=cls.get_queryset
        def wrapper(self):
            queryset = fn(self)
            sortmethod = self.request.GET.get('sort', default_sort)
            if hasattr(queryset, 'order_by'):
                queryset = queryset.order_by(sortmethod)
            return queryset
        cls.get_queryset=wrapper
        return cls
    return decorator

我的问题是,作为整体可读性和可维护性的问题,是否有“更好”或“更可接受”的方式来做到这一点?或者它只是一种编码风格偏好?

我问的原因是,虽然我认为这种方式更具可读性,但我希望有一天能赚钱,并且不想发展那些会让同事如此惹恼的奇怪的编码习惯任何关于编码风格的输入都非常感谢。感谢。

1 个答案:

答案 0 :(得分:3)

这是正确的方法(我编辑了你的代码):

<强> ideapad/views.py

from django.views.generic import ListView
from ideapad.models import Idea
from sauce.decorators import SortQuerysetMixin

class IndexListView(SortQuerysetMixin, ListView):
    default_sort = '-date'
    model = Idea
    template_name = 'ideapad/index.html'
    context_object_name = 'ideas'

<强> sauce/decorators.py

class SortQuerysetMixin(object):
    def get_queryset(self):
        queryset = super(SortQuerysetMixin, self).get_queryset()
        sortmethod = self.request.GET.get('sort', self.default_sort)
        if hasattr(queryset, 'order_by'):
            queryset = queryset.order_by(sortmethod)
        return queryset