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

Not sure what I'm doing wrong

I have also tried the following:

setting product to models.Product.objects.get(pk=1) setting product to models.Product.objects.filter(pk=1) setting product to models.Product.objects.filter(id=1) setting product to models.Product.objects.filter(id__exact=1) having Product.pk=1 in each set

products/emergency.py
from . import models

def fix_25():
    product = models.Product.objects.get(id__exact=1)
    product.review_set.objects.bulk_create([
            Review(ratings=2),
            Review(ratings=3),
            Review(ratings=5)
            ])

1 Answer

Tatiana Vasilevskaya
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Tatiana Vasilevskaya
Python Web Development Techdegree Graduate 28,600 Points

You should use Review.objects.bulk_create([Review(...), Review(...)]) and for each Review specify rating and a product it is related to.

from . import models


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

Yeah that worked. I got the greater concept now. I greatly appreciate your help. Thanks.