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 trialJulian Garcia
18,380 PointsBulk 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.
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
Treehouse Moderator 68,454 PointsYour are on the right track. Here's a few hints:
- to create a Review, you need to specify the
product
with reference to itsid
Through theproduct
keyword argument. The id you referenced was the review's id. - You need to provide the
product=
argument. You will need toget
this Product usingid=1
-
Review
is in themodels
module, it is referenced usingmodels.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'>
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>
Julian Garcia
18,380 PointsChris,
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
Treehouse Moderator 68,454 PointsIt 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
Steven Parker
231,271 PointsSteven Parker
231,271 PointsThanks for the mention. BTW, I got the idea from Treehouse's own JQuery Basics course.
Steven Parker
231,271 PointsSteven Parker
231,271 PointsWell, 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
Treehouse Moderator 68,454 PointsChris Freeman
Treehouse Moderator 68,454 PointsSteven 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