191π
__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
31π
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)
- [Django]-Django URL Redirect
- [Django]-Effects of changing Django's SECRET_KEY
- [Django]-Django JSONField inside ArrayField
14π
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
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
- [Django]-Django rest framework serializing many to many field
- [Django]-How to disable Django's CSRF validation?
7π
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.
- [Django]-Django TypeError: get() got multiple values for keyword argument 'invoice_id'
- [Django]-Token Authentication for RESTful API: should the token be periodically changed?
- [Django]-Where to put business logic in django
6π
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
- [Django]-Using JSON in django template
- [Django]-Django, Models & Forms: replace "This field is required" message
- [Django]-What does "'tests' module incorrectly imported" mean?
4π
You need to define, which column that you want to displayβ¦
for example:
class POAdmin(admin.ModelAdmin):
list_display = ('qty', 'cost', 'total')
- [Django]-Django REST Framework (DRF): Set current user id as field value
- [Django]-Django values_list vs values
- [Django]-Django FileField with upload_to determined at runtime
2π
Youβre right in thinking that __unicode__
does that. I have this running right now:
class Film(models.Model):
title = models.CharField(max_length=200)
...
def __unicode__(self):
return self.title
When I look in the recent actions list, I see the title of the film that I have just edited.
- [Django]-Get the latest record with filter in Django
- [Django]-Django β how to visualize signals and save overrides?
- [Django]-Redirect to named url pattern directly from urls.py in django?
2π
By adding __str__()
method to the model Patient
this way:
class Patient(models.Model):
name=models.CharField(max_length=200)
#.........
def __str__(self):
return self.name
will display name of patient instead object. For detail check here
- [Django]-Django: Get list of model fields?
- [Django]-How to specify an IP address with Django test client?
- [Django]-Django custom field validator vs. clean
1π
Since this question is 6 years old, a lot of things have changed. Let me make an update to it.With python3.6 and the latest version of Django (2.1.2) you should always use __str__()
in new code. __unicode__()
is an old story for python2.7 because in python3, str
is unicode
.
- [Django]-Django β How to rename a model field using South?
- [Django]-Django: How to manage development and production settings?
- [Django]-Whats the difference between using {{STATIC_URL}} and {% static %}