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 Total Control F objects

Lewis Cowles
Lewis Cowles
74,902 Points

How to double rating using F()

So can anyone spot what I cannot in this code? It asks if I used update() OF COURSE I DID; I even checked this with the query expressions docs for Django

products/emergency.py
from django.db.models import F

from . import models


def double_ratings():
    models.Review.objects.all().update(rating=F('rating')*2)

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

While the docs clearly support using .all(), the challenge does not like it.

Tagging Kenneth Love to explain why .all() isn't accepted.

Edit: after Kenneth's checker update, your code passes as-is

Before the edit, the challenge did not want to see ".all()" in the chain. With or without it should now pass the challenge.

Lewis Cowles
Lewis Cowles
74,902 Points

would it be possible for you to remove the answer? Giving people complete canned answers, rather than examples could just teach them to copy-paste

TIA

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

It seems moot since your posted question, together with Kenneth's update, is exactly the solution.

IMO I don't see it as giving completely canned answer when it's correcting code already provided. If the question was was a simple "what's the solution?" I'd see your point.

But as you wish....

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Updated to accept .all(). I left it off initially because it's completely not needed. All queries have an implicit .all() in them until you bring in .except(), .filter(), or .get().

Lewis Cowles
Lewis Cowles
74,902 Points

So does this mean the challenge uses a string check, or regex? It is kinda beside the point, but for sheer curiosity I'd love to know!

The reason I used all is because in the video we used filter, and all. In the Django docs linked in the video, they used all and filter with update. I've used update without before, but I thought Kenneth Love video's were showing me the right way to make the code more readable and explicit, all seemed to provide that.

It's no big, after submitting this, I did remove the call to all and the challenge passed, I just think it's less than ideal that it shouldn't fail a unit test from missing or including other methods (how we'll be testing code we write), so long as they do not affect output, given input, meh ¯_(ツ)_/¯.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

They're graded with a combination of techniques. I check strings, run regex against the source code, and call your code to compare the outputs and errors.