Django Notes-Database operations (many-to-many relationships)

Source: Internet
Author: User

1. Project structure

2. Key code:

data6.settings.py

Installed_apps = (    'Django.contrib.admin',    'Django.contrib.auth',    'Django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'Django.contrib.staticfiles',    'Blog',) DATABASES= {    'default': {        'ENGINE':'Django.db.backends.mysql',        'NAME':'data6',        'USER':'Root',        'PASSWORD':'passwd',        'PORT':'3306',        'HOST':'localhost',    }}
View Code

blog.models.py

 fromDjango.dbImportModelsclassAuthor (models. Model): Name= Models. Charfield (max_length=30)        def __unicode__(self):returnSelf.nameclassBook (models. Model): Name= Models. Charfield (max_length=30) Authors=models. Manytomanyfield (Author)def __unicode__(self):returnSelf.name
View Code

3. Automatically generated data sheet

4. What Django does with the shell:

First, import the objects created in the models :

>>> from blog.models import Author,book

Add author:

>>> Author.objects.create (name= ' Tom01 ')

<Author:Tom01>

>>> Author.objects.create (name= ' Tom02 ')

<Author:Tom02>

>>> Author.objects.create (name= ' Tom03 ')

<Author:Tom03>

>>> Author.objects.create (name= ' Tom04 ')

<Author:Tom04>

To view the generated author:

>>> authors = Author.objects.all ()

>>> Authors

[<author:tom01>, <author:tom02>, <author:tom03>, <author:tom04>]

Add a book Python Book1

>>> B1 = Book ()

>>> b1.name = ' Pyhon Book1 '

>>> B1.save ()

Get an author

>>> tom2 = Author.objects.get (name__exact= ' Tom02 ')

>>> Tom2

<Author:Tom02>

Add author for Python Book1

>>> B1.authors.add (TOM2)

>>> B1.authors.add (authors[3])

the author of B1

>>> B1.authors.all ()

[<author:tom02>, <author:tom04>]

>>> B1.authors.add (authors[2])

Remove an author

>>> B1.authors.remove (authors[3])

>>> B1.authors.all ()

[<author:tom02>]

>>> B1.authors.add (Authors[0])

>>> B1.authors.all ()

[<author:tom01>, <author:tom02>]

>>> b1.authors.filter (name__exact= ' Carl ')

[]

See tom2 have those books

>>> Tom2.book_set.all ()

[<book:pyhon Book1>]

Add a book to tom2

>>> tom2.book_set.create (name= "Java")

<Book:java>

>>> Tom2.book_set.all ()

[<book:pyhon Book1>, <book:java>]

Look at all those books.

>>> books = Book.objects.all ()

>>> Books

[<book:pyhon Book1>, <book:java>]

Get rid of a book

>>> Tom2.book_set.remove (Books[0])

>>> Tom2.book_set.all ()

[<book:java>]

Django Notes-Database operations (many-to-many relationships)

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.