Skip to content
Learn Netverks

Lesson

Step 19/36 53% through track

fields-relationships

Fields and relationships

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Fields and relationships: the syntax, APIs, and habits you need before advancing in Django.

Teams ship Fields and relationships on every Django codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Fields and relationships in contexts like: SaaS dashboards, CMS-style products, internal tools, and APIs paired with React or mobile clients.

Write Python 3 in the editor and click Run on server—the dev runner executes your script; Django framework lessons also use local startproject for full MVT (LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Beyond strings and numbers, Django models express relationships—foreign keys, one-to-one, and many-to-many—keeping relational data normalized.

Common field types

  • CharField, TextField, EmailField, URLField
  • IntegerField, DecimalField, BooleanField
  • DateField, DateTimeField, JSONField
  • FileField, ImageField — uploads to MEDIA_ROOT

Relationships

class Author(models.Model):
    name = models.CharField(max_length=100)

class Article(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    tags = models.ManyToManyField("Tag")

on_delete defines behavior when the related row is deleted—CASCADE removes articles; PROTECT blocks deletion.

Important interview questions and answers

  1. Q: ForeignKey vs ManyToMany?
    A: FK is many-to-one (many articles, one author); M2M is many-to-many (articles and tags).
  2. Q: on_delete=CASCADE risk?
    A: Deleting an author deletes all their articles—sometimes PROTECT or SET_NULL is safer.
  3. Q: Related name?
    A: author.articles_set or custom related_name="articles" for reverse queries.

Self-check

  1. Which relationship links Article to Author?
  2. What does on_delete control?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • ForeignKey on_delete?
  • ManyToMany through?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump