nacho c
nacho c

Reputation: 311

show fields in get all django panel

I am new at django panel, I want to show some particular fields in the index method but I see "NameClass Object" intead. I tried using fields and list_display but it didn't work.

from django.contrib import admin
from base_proyecto.models import *

class AutorAdmin(admin.ModelAdmin):
  fields = ('name', 'dob', 'hp')
  list_display = ('name', 'dob', 'hp')


admin.site.register(Autor)

enter image description here

I want to see multiple columns, one per attribute. It should look like this

Upvotes: 0

Views: 50

Answers (3)

mahendra kamble
mahendra kamble

Reputation: 1395

Write the unicode() method in Author model(or str() if you're using Python3),

class Author(models.Model):
    # Here fields stuff

    def __unicode__(self):
        return self.nomber  

class AutorAdmin(admin.ModelAdmin):
    model = Author
     fields = ('name', 'dob', 'hp')
    list_display = ['name','dob', 'hp']

edit : - if you want to return multiple values then try this

def __unicode__(self):
    return '%s %s %s' % (self.name, self.dob, self.hp)    

Upvotes: 0

nik_m
nik_m

Reputation: 12086

Inside your Auto model you must define a __str__ (for python 3) or __unicode__ (for python 2) method. Like this:

class Autor(models.Model):
    name = models.CharField(max_length=50)
    # other fields here

    def __str__(self):  # python 3
        return self.name

    def __unicode__(self):  # python 2
        return self.name

Upvotes: 1

Andrey Shipilov
Andrey Shipilov

Reputation: 2014

Define a __unicode__() method if you're on python 2 or __str__() if you're on python 3, for your model.

Upvotes: 0

Related Questions