何时在Django CBV中覆盖get方法? [英] When to override get method in Django CBV?

查看:43
本文介绍了何时在Django CBV中覆盖get方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习Django,困惑的根源在于基于类的视图以及何时覆盖get方法。我仔细阅读了文档,并解释了get的作用,但没有解释何时应该覆盖get。



我最初是这样创建视图的:

  class ExampleView( generic.ListView):
template_name ='ppm / ppm.html'
paginate_by = 5

def get(self,request):
profiles_set = EmployeeProfile.objects .all()
context = {
'profiles_set':profilesset,
'title':'Employee Profiles'
}
return render(request,self.template_name ,上下文)

但是最近有人告诉我,我的代码对于默认实现足够简单了,并且我只需要这样:

  class ExampleView(generic.ListView):
模型= EmployeeProfile
template_name ='ppm / ppm.html'

所以我的问题是:在什么情况/情况下我重写了get方法?

解决方案

如果使用内置的通用视图,则几乎不必重写 get() 。您最终可能会复制很多功能,或者破坏视图的功能。



例如, paginate_by 选项在您的视图中将不再起作用,因为您不会在 get()方法中切片查询集。



如果您使用的是基于通用类的视图,例如 ListView ,您应该尽可能覆盖特定的属性或方法,而不是覆盖 get()



您的视图优先于 get()的优点是,它的作用非常清楚。您可以看到该视图获取一个查询集,将其包含在上下文中,然后呈现模板。您无需了解 ListView 即可了解视图。



如果您希望覆盖 get()子类 视图 。您没有使用 ListView 的任何功能,因此对其进行子类化没有任何意义。



<$从django.views.generic import中获取的p $ p>

类ExampleView(View):
template_name ='ppm / ppm.html'

def get(自己,请求):
...


I've been learning Django and one source of confusion I have is with class based views and when to override the get method. I've looked through the documentation and it explains what get does but it doesn't explain when I should override get.

I originally created a view this way:

class ExampleView(generic.ListView):
    template_name = 'ppm/ppm.html'
    paginate_by = 5

    def get(self, request):
        profiles_set = EmployeeProfile.objects.all()
        context = {
            'profiles_set': profiles_set,
            'title': 'Employee Profiles'
        }
        return render(request, self.template_name, context)

But I was recently told that my code was simple of enough for the default implementation, and that all I needed was this:

class ExampleView(generic.ListView):
    model = EmployeeProfile
    template_name = 'ppm/ppm.html'

So my Question is this: In what scenario/circumstance should I override the get method?

解决方案

If you are using the builtin generic views, then you should rarely have to override get(). You'll end up either duplicating lots of functionality, or break features of the view.

For example, the paginate_by option will no longer work in your view, because you are not slicing the queryset in your get() method.

If you are using a generic class based view like ListView, you should try to override specific attributes or methods where possible, rather than overriding get().

The advantage of your view which overrides get() is that it's very clear what it does. You can see that the view fetches a queryset, includes it in a context, then renders the templates. You don't need to know about ListView to understand the view.

If you like the explicitness of overriding get() subclass View instead. You aren't using any of the features of ListView, so it doesn't make sense to subclass it.

from django.views.generic import View

class ExampleView(View):
    template_name = 'ppm/ppm.html'

    def get(self, request):
        ...

这篇关于何时在Django CBV中覆盖get方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆