_exact 精确等于 like ‘aaa’
__iexact 精确等于 忽略大小写 ilike ‘aaa’
__contains 包含 like ‘%aaa%’
__icontains 包含 忽略大小写 ilike ‘%aaa%’,但是对于sqlite来说,contains的作用效果等同于icontains。
__gt 大于
__gte 大于等于
__lt 小于
__lte 小于等于
__in 存在于一个list范围内
__startswith 以…开头
__istartswith 以…开头 忽略大小写
__endswith 以…结尾
__iendswith 以…结尾,忽略大小写
__range 在…范围内
__year 日期字段的年份
__month 日期字段的月份
__day 日期字段的日
__isnull=True/False
class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
商品列表页
"""
# 过滤大于 100 的价格才返回
queryset = Goods.objects.all().filter(shop_price__gt=100)
serializer_class = GoodsSerializer
pagination_class = Pagination
三级列表,默认 ModelSerializer 并不能满足需求
class CategorySerializer3(serializers.ModelSerializer):
class Meta:
model = GoodsCategory
fields = "__all__"
class CategorySerializer2(serializers.ModelSerializer):
sub_cat = CategorySerializer3(many=True)
class Meta:
model = GoodsCategory
fields = "__all__"
class CategorySerializer(serializers.ModelSerializer):
sub_cat = CategorySerializer2(many=True)
class Meta:
model = GoodsCategory
fields = "__all__"
class UpdatesTinkerViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
"""
Tinker 热更新列表
"""
queryset = UpdateTinker.objects.all()
serializer_class = UpdatesSerializer
# 如不指定,默认以 id 为标准
lookup_field = "tinkerId"
http://www.django-rest-framework.org/api-guide/versioning/#varying-behavior-based-on-the-version
def get_serializer_class(self):
if self.request.version == 'v1':
return AccountSerializerVersion1
return AccountSerializer
class GoodsListViewSet(mixins.RetrieveModelMixin, viewsets.GenericViewSet):
# get 方法时,调用一次 点击次数 + 1
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
instance.click_num += 1
instance.save()
serializer = self.get_serializer(instance)
return Response(serializer.data)
只显示当前用户的列表
def get_queryset(self):
return UpdateTinker.objects.filter(user=self.request.user)
class UpdatesTinkerFilter(FilterSet):
"""
商品的过滤filter
"""
applicationId = filters.CharFilter(name="applicationId", lookup_expr='iexact')
version = filters.CharFilter(name="version", lookup_expr='iexact')
class Meta:
model = UpdateTinker
fields = ['applicationId', 'version']
class UpdatesTinkerViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
"""
Tinker 热更新列表
"""
queryset = UpdateTinker.objects.all()
serializer_class = UpdatesSerializer
pagination_class = UpdatesTinkerPagination
filter_backends = (DjangoFilterBackend,)
search_fields = ('applicationId', 'version')
# 查找过滤class
filter_class = UpdatesTinkerFilter
# 默认搜索 tinkerId
lookup_field = "tinkerId"
def validate_name(self, value):
"""
Check that the blog post is about Django.
"""
if 'django' not in value.lower():
raise serializers.ValidationError("Blog post is not about Django")
return value
class Meta:
model = Package
fields = "__all__"
# extra_kwargs = {
# 'sectionName': {'required': False},
# 'client': {'required': False},
# 'applicationID': {'required': False},
# }
# read_only_fields = ("name",)
注册插件
class HelloWorldPlugin(BaseAdminPlugin):
say_hello = False
# 初始化方法根据 ``say_hello`` 属性值返回
def init_request(self, *args, **kwargs):
return bool(self.say_hello)
# 插件拦截了返回 Media 的方法,加入自己需要的 js 文件。
def get_media(self, media):
media.add_js([self.static('/js/test.js')])
return media
xadmin.site.register_plugin(HelloWorldPlugin, ListAdminView)
自己的model 进行拦截
# 友盟管理
class UmengManagerAdmin(object):
...
# 格式 block_xxx
# xxx = 在 xadmin定义模板中的 {% view_block 'top_check_btns' %}
# 需要 view_block 才能插入
def block_top_check_btns(self, context, nodes):
return "Hello world"
# 友盟管理
class UmengManagerAdmin(object):
actions = [MyAction, ]
actions.py
class MyAction(BaseActionView):
# 这里需要填写三个属性
action_name = "my_action" #: 相当于这个 Action 的唯一标示, 尽量用比较针对性的名字
description = (
u'Test selected %(verbose_name_plural)s') #: 描述, 出现在 Action 菜单中, 可以使用 ``%(verbose_name_plural)s`` 代替 Model 的名字.
# model_perm = 'change' #: 该 Action 所需权限
# 而后实现 do_action 方法
def do_action(self, queryset):
result_data = {
'data': '123'
}
return HttpResponse(json.dumps(result_data), content_type="application/json")