DEV Community

loading...
Cover image for Method __str__ Explained using Django Admin Panel

Method __str__ Explained using Django Admin Panel

balt1794 profile image balt1794 ・4 min read

At the moment of creating models in Django, you might notice that almost instinctively everyone adds an extra couple of lines that include the method str or maybe not, but let’s explain the str method anyways.

The method str allows us to convert an object into a string representation. This method is a dunder method which is usually used when creating models in Django, but it’s also used in other places.

Dunder Methods

–> start and end with double underscores.

–>dunder is short for “double under”.

–>another common dunder method is init

If we don’t use the str method, after migrating changes, the admin panel will return the object, but this object will not show a familiar name which will make things difficult when managing objects especially if there are many objects created.

If the method is used, a string representation will be shown in the admin panel. This string will be made up of the field chosen to be returned in the method.

Let’s see an example of the method being used.

For brevity reasons, I have already set up Django plus created an app and a model named Cards.

If you want to know how to do the initial setup of Django and how to create models, please check these posts.

Chapter 1 – Django Setup – (How to create a Django Web App)

Chapter 2 – Django Basics – (How to access the admin panel and create models in Django)

If you just want to understand the method str, carry on reading.

Without str method

The code below shows the model named Cards with two CharFields, concept and description.

Models.py


#path -> cards/models.py

from django.db import models


class Cards(models.Model):
    concept = models.CharField(max_length=75)
    description = models.CharField(max_length=75)



#baltlogs.com

Enter fullscreen mode Exit fullscreen mode

Admin.py

I have imported the Cards model and registered it as well.


#path -> cards/admin.py

from django.contrib import admin
from .models import Cards

admin.site.register(Cards)

#baltlogs.com

Enter fullscreen mode Exit fullscreen mode

If you are following along on your computer, issue the following commands to make migrations and run server.

Screen Shot 2021-03-20 at 7.20.16 PM

Use your credentials and access the admin panel by going to http://127.0.0.1:8000/admin/

If you don’t know how to create a super user and access the admin panel refer to the posts mentioned in the beginning of the tutorial.

Click on the name of the model, in my case, Cards.

Screen Shot 2021-03-20 at 4.52.17 PM

All the entries created that refer to the model (Cards) will be shown here.

If this is the first time you access the admin panel, you willl not have any entries. You can create some by clicking on Add Cards(name of model).

Create some entries and save them. You should see the newly-created entries appear.

In my case, the total count says that I have created 5 cards, but the count next to the card object is up to 12. This is because I have created and deleted a bunch of entries already.

Take a look at the way the objects created are displayed. There’s no difference between them except by the number next to it. However, there’s no way to know which object is which.

The objects appear in this way because we didn’t include the method str

Screen Shot 2021-03-20 at 4.53.02 PM

With str method

Let’s include the str method in models.py.

Models.py


#path -> cards/models.py

from django.db import models


class Cards(models.Model):
    concept = models.CharField(max_length=75)
    description = models.CharField(max_length=75)

    def __str__(self):
        return self.concept



#baltlogs.com

Enter fullscreen mode Exit fullscreen mode

Save the file and issue the commands to make migrations and run the server again.

Go to the admin panel -> Cards(model name)

Screen Shot 2021-03-20 at 4.55.44 PM

You should see the objects displayed, but this time with the string representation of the chosen field of the model, in my case, the concept field.

This string representation makes it easier to identify the objects instead of just having a name such as cards object(#) or the memory address of the object which also shows up sometimes if the the str method is not included.

Some of the most common fields chosen as string representations are the name and title fields. Those fields are a very good way to know which object is which.

Note:

My model is shown as Cardss with a double ‘s’. This is because the admin panel automatically displays the plural of the model name. If I were to name my model Card, then it would appear as Cards in the admin panel.

Learn more about Django:

Twitter

Login/Registration Page with HTML,CSS,& JS Series - PART I

Navbar Styling + Logo using Bootstrap5 & Django

Django 3..2..1..Takeoff Book

Personal Website

Discussion (0)

pic
Editor guide