Django Database Operations-additions and deletions-many-to-many relationships and one-to-many (foreign key) relationships

Source: Internet
Author: User
Tags object object

Django Database Operations-additions and deletions-many-to-many relationships and one-to-many (foreign key) relationshipsoriginal August 29, 2012 14:56:33
    • Label:
    • Django/
    • Database
    • Class/
    • Email/
    • Publishing
    • 28343
One or one-to-many (foreign key)

Example: One author corresponds to multiple books, one book only one author

Model Code:

[Python]View Plain Copy
    1. Class person (models. Model);
    2. Name = models. Charfield (' author name ', max_length=)
    3. Age = Models. Integerfield (' author age ')
    4. Class book (Models. Model):
    5. Person = models. ForeignKey (person, related_name=' Person_book ')
    6. title = models. Charfield (' book name ', max_length=)
    7. Pubtime = models. Datefield (' publication Time ')

(i) Get the object method:

1. Obtaining books from the author

[python] view plain copy

  1. person = Person.objects.fiter (your condition)
  2. Book = Person.book_set.all ()

2. Get authors from books

[python] view plain copy

  1. p = Book.person
Two, many-to-many

Example: One author corresponds to multiple books, one book has multiple authors

Model Code:

[Python]View Plain Copy
    1. Class Author (models. Model):
    2. First_Name = models. Charfield (max_length=)
    3. Last_Name = models. Charfield (max_length=)
    4. email = models. Emailfield ()
    5. Class book (Models. Model):
    6. title = models. Charfield (max_length=)
    7. Authors = models. Manytomanyfield (Author)

(i) Get the object method:

1. Get authors from books

[python] view plain copy

  1. b = Book.objects.get (id=)
  2. B.authors.all ()
  3. B.authors.filter (first_name=' Adam ')

2. Obtaining books from the author



[python] view plain copy

  1. A = Author.objects.get (id=1)
  2. A.book_set.all ()

(ii) Add object method:

[Python]View Plain Copy
    1. A = Author.objects.get (id=1)
    2. b = Book.objects.get (id=)
    3. B.authors.add (a)


(iii) Delete Object object methods:

[Python]View Plain Copy
    1. A = Author.objects.get (id=1)
    2. b = Book.objects.get (id=)
    3. B.authors.remove (a) or B.authors.filter (id=1). Delete ()



Django Database Operations-additions and deletions-many-to-many relationships and one-to-many (foreign key) 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.