# How to convert this raw query to query set?

**URL:** <https://forum.djangoproject.com/t/how-to-convert-this-raw-query-to-query-set/18470>\
**Category:** Using the ORM\
**Created:** [January 27, 2023, 3:52am UTC](https://forum.djangoproject.com/t/how-to-convert-this-raw-query-to-query-set/18470 "2023-01-27T03:52:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fahmiSD](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fahmisd/32/11940_2.png) [@fahmiSD](https://forum.djangoproject.com/u/fahmiSD)\
**Post date:** [January 27, 2023, 3:52am UTC](https://forum.djangoproject.com/t/how-to-convert-this-raw-query-to-query-set/18470/1 "2023-01-27T03:52:11Z")

</div>

I’m new to django, and i want to show field that related to foreign key in another table. this is the table.

![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/3/37165ee7c1eee9cb1377a5dd8af1ce5ebcd86e9e.png)

i want to career table got the career\_tag\_name and hex\_code from table color. i’ve tried the Career.objects.raw() this is the query in views.py:

```auto
careers = Career.objects.raw('''SELECT website_career_tag.career_tag_name,website_color.hex_code, website_career.* 
from website_career INNER JOIN website_career_tag on website_career_tag.id = website_career.career_tag_id_id 
LEFT JOIN website_color on website_career_tag.color_id_id = website_color.ID''')

```

it works perfectly, until i want to use filter() by career\_tag\_name. when i use query set it’s more easy than make it raw to filter.

how do i make those raw query to query set?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 27, 2023, 4:06am UTC](https://forum.djangoproject.com/t/how-to-convert-this-raw-query-to-query-set/18470/2 "2023-01-27T04:06:19Z")

</div>

You say that you are new to Django. Have you worked your way through the official tutorial and done the exercises?

---

<div class="post-metadata">

**Author:** ![fahmiSD](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/fahmisd/32/11940_2.png) [@fahmiSD](https://forum.djangoproject.com/u/fahmiSD)\
**Post date:** [January 27, 2023, 6:41am UTC](https://forum.djangoproject.com/t/how-to-convert-this-raw-query-to-query-set/18470/3 "2023-01-27T06:41:34Z")

</div>

i do, i’ve read about select\_related. throuh, to\_field, etc. but i didn’t get what i want…

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 27, 2023, 11:45am UTC](https://forum.djangoproject.com/t/how-to-convert-this-raw-query-to-query-set/18470/4 "2023-01-27T11:45:37Z")

</div>

So the point with the ORM is that you don’t _need_ to explicitly specify a “join”. You automatically have access to related objects through the ForeignKey field.

Assume you have Models `Career` and `CareerTag`. In `Career` you have a field named `career_tag`, which is a ForeignKey field to `CareerTag`.

Now, lets say that you have an instance of `Career` named `career`. If so, `career.career_name` would be the reference to the String object stored in that column. In the exact same manner, `career.career_tag` would be the reference to the instance of `CareerTag` to which that instance of `Career` refers. Continuing on with that, `career.career_tag.color` is your reference to the `Color` object, and so `career.career_tag.color.color_name` is the reference to that field in `Color`.

Side Note: Django creates the column for a ForeignKey field with an `_id` suffix, giving you access to _either_ the referenced object or the pk field itself. This means you can access or reference either `career.career_tag` or `career.career_tag_id` - which are two different things.

Side note: Since you made the reference to `select_related`, I’ll mention here that that is a performance-enhancement available when you know you’re going to be using related objects in your view. It does not alter how you work with these queried objects.
