Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add more space in `__str__` method of Django model?

Tags:

python

django

I want to add more spaces to __str__ method, but method cut off spaces to 1.

For example:

class Spam(models.Model):
    foo = models.IntegerField()
    bar = models.IntegerField()

    def __str__(self):
        return 'more spaces {}            {}!'.format(self.foo, self.bar)

This output in Django admin: more spaces 1 2

I want : more spaces 1 2

How do this?

like image 940
A. Innokentiev Avatar asked Oct 26 '25 10:10

A. Innokentiev


1 Answers

Where ever your str resides:

from django.utils import safestring

def __str__(self):
    string = safestring.mark_safe("more spaces {}"+" "*10+ "{}!".format(self.foo, self.bar))
    return string
like image 83
Sahil Maniar Avatar answered Oct 28 '25 23:10

Sahil Maniar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!