Django 工具
from django.db.models.signals
- import post_save 保存
- post_delete 删除
from django.dispatch
- import receiver 接受者,接收到之后链接响应 eg: @receiver(post_save) 链接 post_save 信号和处理方法
from django.conf
- import settings 公共的设置类
from django.contrib.sites.models
- import Site 站点信息 clear_site_cache Site.objects.get_current().domain
django.views.decorators.http
- @require_http_methods(["GET", "POST"])
- @require_POST()
- @require_GET()
django.shortcuts
- render(request, template_name[, context字段][, content_type 响应内容类型][, status状态][, current_app当前APP][, dirs][, using])[source]
- render_to_response(template_name[, context][, context_instance][, content_type][, status][, dirs][, using])[source]
- redirect(to, [permanent=False, ]*args, **kwargs)[source]
一个模型(get_absolute_url() ) 一个视图(将使用urlresolvers.reverse 来反向解析名称) 一个绝对的或相对的URL,将原封不动的作为重定向的位置 - get_object_or_404(klass, *args, **kwargs) class 类 参数(可被查询)
get_object_or_404(MyModel, pk=1) - get_list_or_404(klass, *args, **kwargs)
from django.views
- import generic generic.ListView generic.DetailView
快速生成
from rest_framework import routers, serializers, viewsets
Serializers定义了API的表现.
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')
ViewSets 定义了 视图(view) 的行为.
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
Routers 提供了一种简单途径,自动地配置了URL。
router = routers.DefaultRouter()
router.register(r'users', UserViewSet)
from kapp.models import SMURLTypeModel
class SMURLTypeSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = SMURLTypeModel
fields = ('type', 'name')
class SMURLTypeViewSet(viewsets.ReadOnlyModelViewSet):
queryset = SMURLTypeModel.objects.all()
serializer_class = SMURLTypeSerializer
router.register(r'smurltype', SMURLTypeViewSet)