BeginnerCoder

Lesson 17 — Relations

Records that point to other records

A foreign key is a field that stores the id of a record in another table. An order does not copy the user's name; it stores uid: 1, and you look up user 1 when you need the name. In Django: order.user.name.

cities = {7: "Almaty"}
cities[shop["city_id"]]   # 'Almaty'

Note: the exercise runs in your browser without Django installed, so it is a small plain-Python simulation of what Django does for you.

Why it matters: storing the name once means changing it once — every order shows the new name.

Want it another way?
Task

Print each order as "<user name> paid <amount>", looking the name up by uid.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Django Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.