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

Y B
Y B
14,136 Points

Looks like I'm missing something with the bulk_create task for Django ORM?

Not sure what is needed here?

products/emergency.py
from . import models


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

3 Answers

Two things.

1) You need to specify a product instance, not just an id.

2) You need to reference the Review as models.Review if you're only importing models.

Beyond that, your code looks fine.

from . import models

def fix_25():
    product = models.Product.objects.get(id=1)
    models.Review.objects.bulk_create([
        models.Review(product=product, rating=2),
        models.Review(product=product, rating=3),
        models.Review(product=product, rating=5),
    ])
Igor Protsenko
Igor Protsenko
15,651 Points

Can we type :

   models.Review(product__id=1,rating=2),
Y B
Y B
14,136 Points

Yes just tested it that passes.