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

Julian Garcia
Julian Garcia
18,380 Points

Bulk creation. What is wrong with this code?

It shows error and ask me to create Review object using bulk_create. I think this is wrong but i can´t figure out what is going wrong.

products/emergency.py
from . import models
def fix_25():
  models.Review.objects.bulk_create([
    Review(id=1, rating=2),
    Review(id=1, rating=3),
    Review(id=1, rating=5)
    ])

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Your are on the right track. Here's a few hints:

  • to create a Review, you need to specify the product with reference to its id Through the product keyword argument. The id you referenced was the review's id.
  • You need to provide the product= argument. You will need to get this Product using id=1
  • Review is in the models module, it is referenced using models.Review everywhere.

I'll bet you can get it now. But if you're still stuck... <button' onclick='$("#spoiler").slideDown("slow");$(this).remove()'>Press to Reveal Spoiler</button>* <div id='spoiler' style='display:none'>


:warning: SPOILER ALERT


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)
      ])

*JavaScript provided by Steven Parker </div>

Steven Parker
Steven Parker
229,745 Points

Thanks for the mention. BTW, I got the idea from Treehouse's own JQuery Basics course.

Steven Parker
Steven Parker
229,745 Points

Well, darn, @Chris Freeman, there goes our cute spoiler revealer! But I suppose it was also a security hole. Someone was a bit hasty, though, some of the course challenges are now broken.

It wouldn't be hard to implement the spoiler button as a markdown feature, though - maybe you could help promote that? Hey, would you mind emailing me? I'm guessing as a staffer you can get to my address. I have a couple of questions I'd like to ask you.

Thanks!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Steven Parker, I am only a meta-staffer hired to support the new Treehouse Techdegree program. I can't access email address info.

I can be reached at chris.freeman.pdx@gmail.com

Julian Garcia
Julian Garcia
18,380 Points

Chris,

just to be clear, when we are working in shell it is possible to do:

  Review(product=product, rating=2),

but in our project files

  models.Review(product=product, rating=2),

Is that right?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

It depends on how Review is imported.

# if module import is ...
import models
# class reference is then...
models.Review

# if module import is ...
from models import Review
# class reference is then...
Review