Method __str__ Explained using Django Admin Panel
Tags: django, python, webdev, tutorial
Published: 2021-03-21 22:47:31

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)](https://baltlogs.com/chapter-1-django-setup/)

[Chapter 2 – Django Basics – (How to access the admin panel and create models in Django)](https://baltlogs.com/chapter-2-django-basics/)

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**

```python

#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

```

**Admin.py**

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

```python

#path -> cards/admin.py

from django.contrib import admin
from .models import Cards

admin.site.register(Cards)

#baltlogs.com

```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gaizgcp9rb2g7iq8t5l6.png)

Use your credentials and access the admin panel by going to [http://127.0.0.1:8000/admin/](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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1tzfl3o95jaejaqwofu.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/odc4dp4058nlvtfkdycq.png)

**With __str__ method**

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

**Models.py**

```python

#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

```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9c4su0vtpz4td8bfkqf1.png)

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](https://twitter.com/balt1794)  

[Login/Registration Page with HTML,CSS,& JS Series - PART I](https://baltlogs.com/login-registration-page-in-django-using-html-css-javascript-part-i/)

[Navbar Styling + Logo using Bootstrap5 & Django](https://baltlogs.com/navbar-styling-logo-using-bootstrap5-django/#more-1317)

[Django 3..2..1..Takeoff Book](https://amzn.to/34UAIe2)

[Personal Website](https://baltlogs.com)