作家
登录

你应该使用Django admin的9个理由

作者: 来源: 2017-05-23 16:32:58 阅读 我要评论

  •  
  • # admin.py 
  •  
  • class PictureAdmin(admin.ModelAdmin): 
  •  
  •     list_display_fields = ('photo''animal_kind''author''is_promoted', ) 
  •  
  •   
  •  
  • class AuthorAdmin(admin.ModelAdmin): 
  •  
  •     list_display_fields = ('name''email', ) 
  •  
  •   
  •  
  • class CommentAdmin(admin.ModelAdmin): 
  •  
  •     list_display_fields = ('picture''author', )  
  • Tip #2: 按你爱好的方法筛选你的数据

    很多人应用 Django admin 后台对指定字段进行筛选。要知道,把一个字段名放到 list_filter 列表里就可以了。同时它也异常轻易地创建一个自定义过滤器!

    1. class ProductiveAuthorsFilter(admin.SimpleListFilter): 
    2.  
    3.     parameter_name = 'is_productive' 
    4.  
    5.     title = 'Productive author' 
    6.  
    7.     YES, NO = 1, 0 
    8.  
    9.   
    10.  
    11.     # Number of comments for an author to be considered a productive one 
    12.  
    13.     THRESHOLD = 100 
    14.  
    15.   
    16.  
    17.     def lookups(self, request, model_admin): 
    18.  
    19.         return ( 
    20.  
    21.             (self.YES, 'yes'), 
    22.  
    23.             (self.NO'no'), 
    24.  
    25.         ) 
    26.  
    27.   
    28.  
    29.     def queryset(self, request, queryset): 
    30.  
    31.         qs = queryset.annotate(Count('comments')) 
    32.  
    33.   
    34.  
    35.         # Note the syntax. This way we avoid touching the queryset if our 
    36.  
    37.         # filter is not used at all

        推荐阅读

        你会使用交换机吗?连接交换机的正确方法

      连接交换机的办法 互联网普及的时代,浩瀚家庭中都有各类上彀的设备,然则平日家里只有一条宽带入户,那么在>>>详细阅读


      本文标题:你应该使用Django admin的9个理由

      地址:http://www.17bianji.com/lsqh/35374.html

    关键词: 探索发现

    乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

    网友点评
    自媒体专栏

    评论

    热度

    精彩导读
    栏目ID=71的表不存在(操作类型=0)