zoukankan      html  css  js  c++  java
  • Django querydict

    >>> js=JsonModels.objects.filter(id=1).values("names").first()
    >>> js
    {'names': '1111'}
    >>> type(js)
    <class 'dict'>
    >>> js=JsonModels.objects.filter(id=1).values("names").first()
    >>> type(js)
    <class 'dict'>
    >>> js=JsonModels.objects.filter(id=1).values("names")
    >>> type(js)
    <class 'django.db.models.query.QuerySet'>

    ########## querydict

    >>> bs=js=JsonModels.objects.filter(id=1)
    >>> bs
    <QuerySet [<JsonModels: JsonModels object (1)>]>
    >>> bs.first()
    <JsonModels: JsonModels object (1)>
    >>> bo=bs.first()
    >>> bo.names
    '1111'
    >>>

    doc:

    for e in Entry.objects.all():
        print(e.headline)
    ## queryset to list dict
    entry_list = list(Entry.objects.all())


    annotate()

    annotate* args** kwargs

    QuerySet使用提供的查询表达式列表注释中的每个对象表达式可以是简单值,也可以是对模型(或任何相关模型)上字段的引用,也可以是针对与对象中的对象相关的对象计算出的聚合表达式(平均值,总和等)。QuerySet

    的每个参数annotate()都是一个注释,该注释将添加到QuerySet返回的对象中

    Django提供的聚合功能在下面的“聚合功能”中进行了描述

    使用关键字参数指定的注释将使用关键字作为注释的别名。匿名参数将根据聚合函数的名称和要聚合的模型字段为其生成别名。只有引用单个字段的聚合表达式才可以是匿名参数。其他所有内容都必须是关键字参数。

    例如,如果您要处理博客列表,则可能要确定每个博客中有多少个条目:

    >>> from django.db.models import Count
    >>> q = Blog.objects.annotate(Count('entry'))
    # The name of the first blog
    >>> q[0].name
    'Blogasaurus'
    # The number of entries on the first blog
    >>> q[0].entry__count
    42
    

    Blog模型本身并没有定义entry__count属性,但是通过使用关键字参数指定聚合函数,您可以控制注释的名称:

    >>> q = Blog.objects.annotate(number_of_entries=Count('entry'))
    # The number of entries on the first blog, using the name provided
    >>> q[0].number_of_entries
    42
    

    有关聚合的深入讨论,请参阅有关聚合的主题指南

    https://docs.djangoproject.com/en/3.0/ref/models/querysets/

     
  • 相关阅读:
    如何将DataTable转换成List<T>
    关于SqlDataAdapter的使用
    VS 2010中JS代码折叠插件
    ASP.net中的几种分页方法
    学习jquery基础教程的一些笔记
    js中innerHTML与innerText的用法与区别
    SpringBoot 中使用shiro注解使之生效
    redis分布式锁
    使用ZSetOperations(有序)操作redis
    使用SetOperations(无序)操作redis
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/14031171.html
Copyright © 2011-2022 走看看