__unicode__
does do that. Your model should look something like this:
class SomeModel(models.Model):
def __unicode__(self):
return 'Policy: ' + self.name
On Python 3 you need to use __str__
:
def __str__(self):
return 'Policy: ' + self.name
Using the __str__
method works on Python3 and Django1.8:
class MyModel(models.Model):
name = models.CharField(max_length=60)
def __str__(self):
return 'MyModel: {}'.format(self.name)
The string you're seeing is coming from __unicode__
method, as others have mentioned. But the thing is that admin saves string representation of an object when it creates log event, therefore if you add __unicode__
implementation after the log entry was saved, you won't see new titles on old items, only after you make some new activity
The answers mentioning __str__
and __unicode__
methods are correct. As stated in the docs however, since version 1.6 (I think), you can use the python_2_unicode_compatible
decorator for both Python 2 and Python 3:
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class MyClass(models.Model):
def __str__(self):
return "Instance of my class"
You can use the above in non-Model
objects as well.
This would work, using def str(self): which returns self.title
Use something like:
class Blog(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
You need to define, which column that you want to display...
for example:
class POAdmin(admin.ModelAdmin):
list_display = ('qty', 'cost', 'total')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With