The use of ORM to the Database library table related operations

Source: Internet
Author: User

Way One:

# create方法的返回值book_obj就是插入book表中的python葵花宝典这本书籍纪录对象

   book_obj = Book.objects.create(title = "python葵花宝典" ,state = True ,price = 100 ,publish = "苹果出版社" ,pub_date = "2012-12-12" )Way two: book_obj = Book(title = "python葵花宝典" ,state = True ,price = 100 ,publish = "苹果出版社" ,pub_date = "2012-12-12" ) book_obj.save()  # 对象.save()是必须有的 recommended Way OneQuery Database Common methods = = "and query request body inside the attribute is different, pay attention to distinguish, query request body." Post is a queryset, where the Get method does not find the value of the default return to None, and the query database get not the object will be directly error.    book_obj = Book.objects.方法(参数视方法而定 )
1 <1>all (): Query all results2   3 <2>Filter (**kwargs): It contains objects that match the filter criteria given4   5 <3>Get (**kwargs): Returns an object that matches the filter criteria given, with only one return result,6 If there are more than one object that meets the filter criteria or no error is thrown. 7   8 <4>Exclude (**kwargs): It contains objects that do not match the given filter criteria9  Ten <5>order_by (*field): Sorting Query Results One    A <6>reverse (): Reverse order of query results -    - <8>count (): Returns the number of objects in the database that match the query (QuerySet).  the    - <9>First (): Returns the number one record -    - <Ten>last (): Returns the final record +    - < One>exists (): Returns True if Queryset contains data, otherwise false +   A < A>VALUES (*field): Returns a valuequeryset--a special queryset, which is not a series of at an instantiated object of model, but an iterative dictionary sequence - < ->values_list (*field): It is very similar to the values (), it returns a tuple sequence, and values returns a dictionary sequence -   - < ->Distinct (): Remove duplicate records from the returned results

Fuzzy query based on double underline

1Book.objects.filter (price__in=[100,200,300])2Book.objects.filter (price__gt=100)3Book.objects.filter (price__lt=100)4Book.objects.filter (price__range=[100,200])5Book.objects.filter (title__contains="python")6Book.objects.filter (title__icontains="python")7Book.objects.filter (title__startswith="py")8Book.objects.filter (pub_date__year=2012)

Delete a table record

Let's start with a direct comment, then makemigrations== "migrate directly into, the following directly call the encapsulated method, a bit easier

Note: You cannot call the table name directly. objects to delete!!! Look underneath.

1 The deletion method is delete (). It runs immediately when the object is deleted without returning any values. For example:2 3 4 Model_obj.delete ()5 You can also delete multiple objects at once. Each QuerySet has a delete () method that deletes all the objects in QuerySet at once. 6 7 For example, the following code removes pub_date as the Entry object for 2005:8 9Entry.objects.filter (pub_date__year=2005). Delete ()Ten when Django deletes an object, it mimics the behavior of the SQL constraint on DELETE CASCADE, in other words, deleting an object also deletes the foreign key object associated with it. For example: One  A  -b = Blog.objects.get (pk=1) - #This would delete the Blog and all of its Entry objects. the B.delete () - Note that the Delete () method is a method on QuerySet, but does not apply to the Manager itself. This is a protection mechanism to avoid accidentally invoking the Entry.objects.delete () method, which causes all records to be mistakenly deleted. If you confirm that you want to delete all the objects, then you must explicitly call: -  -  + Entry.objects.all (). Delete () - If you do not want to cascade Delete, you can set it to: +  A  atPubhouse = models. ForeignKey (to='Publisher', On_delete=models. Set_null, Blank=true, Null=true)

Modify Table Records

Book.objects.filter (title__startswith="py"). Update (PRICE=120)

In addition, the update () method is valid for any result set (QuerySet), which means that you can update multiple records at the same time the update () method returns an integer value that represents the number of record bars affected.

The use of ORM to the Database library table related operations

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.