示例#1
0
def update_field_value():
    Blog.objects.create(name=' '.join(fake.words(2)))

    with count_queries():
        blog = Blog.objects.get()
        blog.likes = blog.likes + 1
        blog.save()
示例#2
0
def related_object_count__annotation():
    create_blog_posts(300)

    with count_queries():
        for author in Author.objects.annotate(
                blogpost_count=Count('blogpost')):
            print(author.name, '||', author.blogpost_count)
示例#3
0
def reuse_querysets():
    create_blog_posts(1)

    with count_queries():
        if Blog.objects.all():
            print('There are blogs!')
            for blog in Blog.objects.all():
                print(blog.name)
示例#4
0
def reuse_querysets__reuse():
    create_blog_posts(1)

    with count_queries():
        blogs = Blog.objects.all()
        if blogs:
            print('There are blogs!')
            for blog in blogs:
                print(blog.name)
示例#5
0
def following_reverse_fk():
    create_blog_posts(300)

    with count_queries():
        for author in Author.objects.all():
            print(author.name)

            for blog_post in author.blogpost_set.all():
                print(blog_post.title, '||', blog_post.blog.name)
示例#6
0
def bulk_create():
    blog = Blog.objects.create(name=' '.join(fake.words(2)))
    author = Author.objects.create(name=fake.name())

    with count_queries():
        for x in range(300):
            BlogPost.objects.create(
                title=' '.join(fake.words(5)),
                blog=blog,
                author=author,
            )
示例#7
0
def following_reverse_fk__prefetch_related__select_related():
    create_blog_posts(300)

    with count_queries():
        authors = Author.objects.prefetch_related(
            Prefetch(
                'blogpost_set',
                queryset=BlogPost.objects.select_related('blog'),
            ))
        for author in authors:
            print(author.name)

            for blog_post in author.blogpost_set.all():
                print(blog_post.title, '||', blog_post.blog.name)
示例#8
0
def following_m2m__prefetch_related():
    create_blog_posts(300)

    with count_queries():
        for blog_post in BlogPost.objects.prefetch_related('tags'):
            print(', '.join((tag.name for tag in blog_post.tags.all())))
示例#9
0
def following_foreignkeys__large__select_related():
    create_blog_posts(300)

    with count_queries():
        for blog_post in BlogPost.objects.select_related('author'):
            print(blog_post.author.name)
示例#10
0
def following_foreignkeys__large():
    create_blog_posts(300)

    with count_queries():
        for blog_post in BlogPost.objects.all():
            print(blog_post.author.name)
示例#11
0
def following_foreignkeys__small__select_related():
    create_blog_posts(1)

    with count_queries():
        blog_post = BlogPost.objects.select_related('author').get()
        print(blog_post.author.name)
示例#12
0
def update_field_value__f():
    blog = Blog.objects.create(name=' '.join(fake.words(2)))

    with count_queries():
        Blog.objects.filter(id=blog.id, ).update(likes=F('likes') + 1, )
示例#13
0
def related_object_count():
    create_blog_posts(300)

    with count_queries():
        for author in Author.objects.all():
            print(author.name, '||', author.blogpost_set.count())