How to Convert a Database Object to a String in Django 


               


How to Convert a Database Object to a String in Django



Python


In this article, we show how to convert a database object to a string in Django using the __str__ function.

So why would we need to do this? What's the importance of doing this?

So, say, you create a table and this table is called Students, representing students at a school, for instance.

This table has 3 columns, the first name, last name, and email address of a student.

When we add a row to a table representing a student (first name, last name, email address), this student, this row of data, is an object in Python.

Let's say, we enter the student, Joyce Peters whose email address is abc@myemail.com. And let's say we have another student named Michael Robertson whose email addres is def@myemail.com. These are objects in Python.

Now, if you use the line, Students.objects.all(), in order to see the objects in the database table, this is what you would get as output.

Django database objects

You see that you don't get the student information. Instead, you all you get is that there are 2 objects in the database table. You don't know the students' names or email addresses. You have no information other than the fact that there are 2 objects in the table.

This is why we want to convert an object to a string.

With conversion to a string, we can represent the student's information such as the name and email address, and not just get returned an object.

So, below was the original code in the models.py file in the Articles app that simply returns object when you have the line, Students.objects.all()



In the above code, we have no implemented any object to string conversion. This is why when you run the code, Students.objects.all(), you simply get returned objects (this is after you have inserted 2 rows, which I did through the Django database API).

Now, below we make a function using __str__ to convert a database object (a row representing a student) to a string.

And you can customize this any way you want. If you just want to show the student's firstname, you can. If you want to show simply the first and last name, you can. However, I want to show all columns, so I'm going to show the first and last name, as well as the email address.

So, the following code does this.



So, the whole first block of code up to email is the same block of code as before.

The difference is now the __str__ function.

So we define a function __str__ with self passed in as a parameter, of the model (table) referring to itself.

This __str__ function uses the return keyword to return the first name, last name, and email of the student.

Again, if you just want to return the first name, your code would be, return self.firstname

If you just want to return the first and last name, the code would be, return self. firstname + ' ' + self.lastname.

It's fully customizable to your preference. However, I chose to include the student's full name, as well as email.

Now the object is represented as a string.

A few important things to know is the __str__ must be indented in the code. If it is not indented in line with the Students class, then Python will not know that you're referencing that function. The __str__ must be indented in line with the class that it is referring. This is because you could have multiplied classes. How would Python know which class you are referring to without proper indentation.

Another thing you must be aware of is that you must exist the database API interactive shell in order for Django to give the new output. Or else, it will store the old structure before you created this function. So, you must close out of it with the exit() or just closing the command prompt and re-opening.

Once you have done this, you import the Students table and write the line, Students.objects.all() into the command prompt.

This will now produce the following code, shown below.

Database Object to String Conversion with __str__ function

So, now you see we are no longer returned object. We can now see the full student information including the first name, last name, and email address.

So this is the power of database object to string conversion in Python and Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...