您的位置:首页 > 编程语言 > Go语言

Django 函数调用路线图

2017-12-08 19:29 323 查看


QuerySet的三个层次:

第一个:

显示所有的objects:

class ExampleView(ListView):

model = Example

第二个:

显示满足查询条件的子集:

class ExampleView(ListView):

queryset = Example.objects.filter(fieldname='something')

或者用get_queryset()

class ExampleView(ListView):

def get_queryset(self):

return Example.objects.filter(...)

第三个:get_object()

get_object()返回单个的object。如果有了queryset,则将其作为object数据源,否则,就会调用get_queryset()获取数据源。get_object()首先搜寻视图中的pk_url_kwarg参数,如果找到,就根据其进行主键查找(primary-key based)。如果没有pk_url_kwarg,就搜寻slug_url_kwarg参数,然后针对slug_field进行slug查询。如果query_pk_and_slug为True,则进行主键和slug的组合查询。


视图类中方法的调用顺序

dispatch()永远是第一个被调用的方法(除了__init__())它接收一个request参数以及关键词参数(*args, **kwargs),并且触发相应的get()或者post()方法。

ListView:

Method Flowchart:
dispatch()
http_method_not_allowed()
get_template_names()
get_queryset()
get_context_object_name()
get_context_data()
get()
render_to_response() 

DetailView:

Method Flowchart

dispatch()
http_method_not_allowed()
get_template_names()
get_slug_field()
get_queryset()
get_object()
get_context_object_name()
get_context_data()
get()
render_to_response()

Base Views:

View:

dispatch()
http_method_not_allowed()
options()


TemplateView:

dispatch()
http_method_not_allowed()
get_context_data()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  django 执行顺序