Reputation:
This is my github repo Inout. I am learning django and i worked Very very simple django registration & login system.
My question is:
How to list all the usernames
in admin
using list_display
. But nothing display in admin panel. May i know why ?
Inside my working code:
# models.py
username = models.OneToOneField(User)
first_name = models.CharField(max_length=100)
# admin.py
class SignupAdmin(admin.ModelAdmin):
list_display = ['username']
admin.site.register(Signup, SignupAdmin)
Information for you Reference :
if i am using list_filter
in admin i can see all the username in the filter panel
Then if i am accessing this page http://127.0.0.1:8000/admin/system/signup/
Select signup to change
0 signups
And also if i am accessing this page http://127.0.0.1:8000/admin/frontend/profile/add/
i can see the drop down of username
shows all the username i registered before.
What i missing
? or can somebody clone
my repo
and see yourself.
Upvotes: 0
Views: 1389
Reputation: 17731
Are you sure it's not working correctly? list_display
is supposed to take a tuple/list of fields and then display those fields as columns of the main table like in the picture shown below taken from the django admin documentation, where each entry in the main table has a username, email address, first name, last name, staff status. This would be created by
list_display = ['username', 'email', 'first_name', 'last_name', 'is_staff']
in a ModelAdmin for the built in User model (taken from django.contrib.auth.models
). The side-column on the right side (with the label "Filter") is populated only when you define fields under list_filter
.
Note if you only defined one field, and your model has a __unicode__
function that returns the username, you will not see a significant difference with just adding list_display = ('username',)
. I suggest you try list_display = ('username', 'first_name',)
. In this case, for every SignUp
you will see two columns in the main table -- one with the username
and one with the first_name
.
EDIT
You have two errors.
First, you don't seem to have created any SignUp
objects anywhere. Before the admin change list will display any entries, you must create some entries.
Second, your __unicode__
method of your SignUp model refers to non-existent fields (self.user is never defined -- in your SignUp class you used username = models.OneToOneField(User)
, hence you refer to it as username
) and furthermore it doesn't return a unicode string as required.
Try:
def __unicode__(self):
if self.username:
return unicode(self.username)
then create some SignUp and then it will work. Again, the list_display part was working perfectly.
Upvotes: 1