Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django ORM Basic ORM Usage Bulk create

Nathan Magyar
Nathan Magyar
11,332 Points

bulk_create for Reviews with Product ID of 1

I'm not sure how to specify the product ID for each review.

I've tried the following, and none of them work: ... Review(product.id=1, rating=2) Review(product_id=1, rating=2) Review(product__id=1, rating=2) ...

Thanks in advance!

products/emergency.py
from . import models

def fix_25():
    models.Review.objects.bulk_create([
        Review(product__id=1, rating=2),
        Review(product__id=1, rating=3),
        Review(product__id=1, rating=5),
    ])

1 Answer

Since you imported models you have to refer to models.Review. (Or you could do from .models import Review and then just refer to Review.) Also, product_id has only one underscore and you don't need the extra comma after the third item. (That shouldn't throw an error but delete just in case.) So it should be:

def fix_25():
    models.Review.objects.bulk_create([
        models.Review(product_id=1, rating=2),
        models.Review(product_id=1, rating=3),
        models.Review(product_id=1, rating=5)
    ])