0Pricing
Django Academy · Lesson

str and the Meta Class

Make objects readable and set table behavior.

str and the Meta Class is a free Django Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Make Objects Readable

Out of the box, a model object prints as a vague Object 1. You can fix that and also shape table behavior cleanly.

Define __str__

Add a __str__ method to return a friendly label. Now your object shows its title instead of a useless number.

class Book(models.Model):
    title = models.CharField(max_length=200)

    def __str__(self):
        return self.title

Where __str__ Shows Up

That __str__ label appears in the admin, the shell, and templates, making every object instantly recognizable. 👀

Always Return a String

Your __str__ must return a string. If a field might be a number, wrap it with str() before returning it.

def __str__(self):
    return str(self.year)

Meet the Meta Class

An inner Meta class holds options about the model as a whole, separate from its fields. It tunes how the table behaves.

class Book(models.Model):
    class Meta:
        ordering = ["title"]

ordering Sets Default Sort

The ordering Meta option sets how query results sort by default, so lists come back in a predictable order.

class Meta:
    ordering = ["-created"]

Friendly Names with verbose_name

Use verbose_name and verbose_name_plural in Meta to control the labels Django shows in the admin interface.

class Meta:
    verbose_name_plural = "Books"

Custom Table Name

Override the generated table name with db_table in Meta when you need to match an existing or legacy database.

class Meta:
    db_table = "catalog_books"

Cross-Field Uniqueness

The unique_together Meta option requires a combination of fields to be unique, like one slug per author.

class Meta:
    unique_together = ["author", "slug"]

Meta Describes the Model

Think of Meta as settings for the whole model, while fields and __str__ describe its data and display.

Both Are Optional but Worth It

You can skip __str__ and Meta, but adding them makes your models far nicer to work with day to day.

Quick Check

What is the main job of the __str__ method on a model?

Recap: __str__ and Meta

You learned that __str__ gives objects a readable name and the Meta class tunes model-wide behavior like ordering and names. Polished models! 🎉

Frequently asked questions

Is the “str and the Meta Class” lesson free?

Yes — the full text of “str and the Meta Class” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “str and the Meta Class”?

Make objects readable and set table behavior. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “str and the Meta Class” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining a Model Class
  2. Choosing Field Types
  3. Field Options: null, blank, default
  4. str and the Meta Class
← Back to Django Academy