I'm building a website using python and Django, but when I looked in the admin, the names of the model items aren't showing up.
So, the objects that I am building in the admin aren't showing their names. admin.py:
from .models import Article, Author
# Register your models here.
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
list_display = ['title', 'main_txt', 'date_of_publication']
list_display_links = None
list_editable = ['title', 'main_txt']
def __str__(self):
return self.title
@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
list_display = ['first_name', 'last_name', 'join_date', 'email', 'phone_num']
list_display_links = ['join_date']
list_editable = ['email', 'phone_num', ]
def __str__(self):
return f"{self.first_name} {self.last_name[0]}"
models.py:
# Create your models here.
class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
date_of_birth = models.DateField()
email = models.CharField(max_length=300)
phone_num = models.CharField(max_length=15)
join_date = models.DateField()
participated_art = models.ManyToManyField('Article', blank=True)
class Article(models.Model):
title = models.CharField(max_length=500)
date_of_publication = models.DateField()
creaters = models.ManyToManyField('Author', blank=False)
main_txt = models.TextField()
notes = models.TextField()
CodePudding user response:
Add __str__()
method in the model itself instead of admin.py, so:
class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
date_of_birth = models.DateField()
email = models.CharField(max_length=300)
phone_num = models.CharField(max_length=15)
join_date = models.DateField()
participated_art = models.ManyToManyField('Article', blank=True)
def __str__(self):
return f"{self.first_name} {self.last_name}"
class Article(models.Model):
title = models.CharField(max_length=500)
date_of_publication = models.DateField()
creaters = models.ManyToManyField('Author', blank=False)
main_txt = models.TextField()
notes = models.TextField()
def __str__(self):
return self.title
CodePudding user response:
You need to specify a def __str__(self)
method.
Example:
class Author(models..):
....
def __str__(self):
return self.first_name ' ' self.last_name