[Django]-Python Django: Getting output as "task object(1)" instead of actual value in model table

3👍

In your django model’s class, you have this, which is correct:

 def __str__(self):
    return "%s %s"%(self.task, self.completed)

That will return whatever you want in the Django admin (in the case above, it will return whatever is in the Task and Completed columns for that row).

In your code above, however, the __str__ is not indented enough so it is not part of the class for that model. Make sure you properly indent it. So, it should look like this:

class Taskdb(models.Model):
    task = models.CharField(max_length = 30)
    priority = models.CharField(max_length = 30)
    completed = models.BooleanField(default=False)
    time_date = models.DateTimeField(default=timezone.now)


    def __str__(self):
        return "%s %s"%(self.task, self.completed)

There are also other attributes you can put in the model’s Meta class to change they way things display in the Djang admin.

For more information: https://www.geeksforgeeks.org/customize-django-admin-interface/

Leave a comment