Python (Django) Classroom Series – 07/Sept/2021

Creating Records for a Many to One Relationship

  • Look at the code below Preview

Creating Records for a Many to Many Relationship

from books.models import Book, Contributor
author = Contributor.objects.create(first_name='Brian', last_name='Young', email='test@test.com')
publisher = Publisher.objects.get(id=1)
book = Book.objects.get(title='Your brain at work')
book.contributors.add(author, through_default={'role': 'Author'})
  • Exercise: Create the records from django shell to insert records for the followings
    • Rich Dad Poor Dad
    • The ART of Computer Programming

Returning an Object using get() method

Preview Preview

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin