Orm:object Relationship Mapping

Source: Internet
Author: User

Orm:object relationship Mapping Two commands about the database: Python manage.py makemigrationspython manage.py Migrate Database The first configuration must complete the migration of the database, Executing the top two commands is a good way to record the form: 1. Add obj = models. Table name (**kwargs) Obj.save () models. Table name. Objects.create (**kwargs) 2, Modify # method one: obj = Models. Table name. Objects.filter (name= "python") [0]obj.name = "JAVA" obj.save# mode two updatemodels. Table name. Objects.filter (name= " Python "). Update (**kwargs) 3, Delete: Models. Table name. Objects.filter (name=" python "). Delete () 4, Query: Queryset Collection object: Models. Table name. Objects.all () models. Table name. Objects.filter (**kwargs) models. Table name. Objects.all (). VALUES (*args) Models. Table name. Objects.all (). Values_list (*args) models. Table name. Objects.exclude (**kwargs) models. Table name. objects.order_by (*args) Models. Table name. OBJECTS.DISTINCT (*args) Model object: models. Table name. Objects.get (**kwargs) models. Table name. Objects.filter (**kwargs). First () models. Table name. Objects.filter (**kwargs). Last () models. Table name. Objects.filter (**kwargs). Count () Models. Table name. Objects.filter (**kwargs). exist () association table Logging Operations: Table relationships: 1, one-to-2, a-to-more 3, many-to-many add records: A-to-more: Create a pair of more: publish = models. ForeignKey ("Publish", Related_name= "Booklist") #添加记录方式1: Models. Book.Objects.create (publish_id=1) #添加的记录方式2:p ubobj = models. Publish.objects.filter (name = "Publishing House") [0]models. Book.objects.create (Publish=pubobj) Many-to-many: Create a many-to-many relationship: Authorlist = models. Manytomanyfield ("Author", Related_name= "Booklist") many-to-many add records: Book_obj = models. Book.objects.create (title= "Herd Man", price=100,publishdate= "2017-12-12", publish_id=2) Alex_obj = models. Author.objects.filter (name= "Alex") [0]egon_obj = models. Author.objects.filter (name= "Egon") [0] query record: Forward query: One-to-many: Linux_obj = models. Book.objects.filter (title= "Linux"). First () print (linux_obj.publish.name) #与这本书对象关联的出版社的对象的名字多对多: Linux_obj = Models . Book.objects.filter (title= "Linux"). First () print (Linux_obj.authorlist.all ()) # Object collection of all authors associated with this book reverse query: one-to-many: eg: find the name of the book published by the publishing house Publist_obj = models. Publish.objects.filter (name= "the Publishing house") [0]print (Publist_obj.bookList.all ()) # Publishing House book object collection Many-to-many: eg: find Yuan published the name and price of books known Author_yuan = models. Author.objects.get (name= "Yuan") print (Author_yuan.bookList.all ()) collection of objects for all books associated with the author

  

Orm:object Relationship Mapping

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.