Django combined with Mongoengine for MongoDB operation (ii)

Source: Internet
Author: User

Tag: equals doc import sum imp UK count () string border

1. Get Data

For post in Post.objects:print (Post.title) for post in post.objects:
Print (Post.title)
Print (' = ' * Len (post.title))
If Isinstance (post, Textpost):
Print (post.content)
If Isinstance (post, Linkpost):
Print (' Link: {} '. Format (Post.link_url))

# Show DataFor post in Post.objects:print (Post.title) for post in post.objects:
Print (Post.title)
Print (' = ' * Len (post.title))
If Isinstance (post, Textpost):
Print (post.content)
If Isinstance (post, Linkpost):
Print (' Link: {} '. Format (Post.link_url))

# Get specific data content for post in post.objects (tags= ' MongoDB '): Print (post.title) # Get the number of specific data content num_posts = post.objects (tags= ' Mon Godb '). Count () print (' Found {} posts with tag ' MongoDB '. Format (num_posts)) # Author's country (foreign key) uk_pages = Page.objects (author__ country= ' UK ')-ne-unequal-lt-less-LTE-less than equals-GT-greater-than-GTE-greater than or equal-not-take inverse Q (age__not__mod=5)-in-value in List-nin-value not in list- MoD-modulo (remainder)-all-same as the value of the list-size-sizes of the array-exists-the value of the field exists Young_users = Users.objects (age__lte=18) string Query-exact– string field exactly matches this value-iexact– string field exactly matches this value (case sensitive)-The contains– string field contains this value-the icontains– string field contains this value (case sensitive)-startswith– The string field starts with this value-the istartswith– string field starts with this value (case sensitive)-the endswith– string field is terminated by this value-the iendswith– string field is terminated by this value (case sensitive)-match– execution $elemMa TCH operation, so you can use the document instance in an array of users = user.objects[10:15]
Users = User.objects.skip (Ten). Limit (5) summation: Yearly_expense = Employee.objects.sum (' salary ') averaging: Mean_age = User.objects.average (' age ') asks which ages (de-weight) are included all_age = User.objects.distinct (' age ') to remove certain conditions (not including ages less than 18) User.objects.exclude (age__lt=18) gets only one field User.objects.only (' Age ') user.objects (age=12). Only (' name '). First () # Define the Query method class blogpost (Document):
title = Stringfield ()
Published = Booleanfield ()
@queryset_manager
def live_posts (Doc_cls, Queryset):
Return Queryset.filter (published=true) blogpost (title= ' test1 ', Published=false). Save () blogpost (title= ' test2 ', published=true). Save () Assert Len (blogpost.objects) = = 2assert Len (blogpost.live_posts ()) = = 1 # get only the field data you want >>> Class Film (Document): ... title = Stringfield () ... year = Intfield () ... rating = Intfield (default=3) ...>> > Film (title= ' The Shawshank Redemption ', year=1994, rating=5). Save () >>> f = Film.objects.only (' title '). First () >>> F.title ' The Shawshank Redemption ' >>> f.year # none>>> f.rating # Default Value3 Advanced QuerySometimes multiple conditions need to be combined, and the method mentioned above will not meet the requirements. You can then use the Q Class of Mongoengine. It can carry multiple query criteria & (with) and | (or) operation. For example, the following statement is a query for all British users older than or equal to 18 years old, or all users older than or equal to 20 years old. From mongoengine.queryset.visitor import q user.objects ((q (country= ' UK ') & Q (age__gte=18)) | Q (AGE__GTE=20))

# Sort from datetime import datetime
Class blogpost (Document):
title = Stringfield ()
Published_date = Datetimefield ()
Meta = {
' Ordering ': ['-published_date ']
}blog_post_1 = blogpost (title= "blog post #1") Blog_post_1.published_date = DateTime (1, 5, 0, 0, 0) blog_post_2 = Blog Post (title= "blog post #2") Blog_post_2.published_date = DateTime (1, 6, 0, 0, 0) blog_post_3 = blogpost (title= "blog Po St #3 ") Blog_post_3.published_date = DateTime (2010, 1, 7, 0, 0, 0)
Blog_post_1.save () Blog_post_2.save () Blog_post_3.save ()
# get the "first" blogpost using default ordering# from BlogPost.meta.orderinglatest_post = BlogPost.objects.first () Asser T latest_post.title = = "Blog post #3" # Override default ordering, order blogposts by "published_date" First_post = blogpost . objects.order_by ("+published_date"). First ()

Django combined with Mongoengine for MongoDB operation (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.