RonCajan
RonCajan

Reputation: 139

How to show column name on admin site in django?

Admin site user table I'm extending my admin table on django default admin table. After adding a column, when I go to the admin site, I can only see the username column name on the user section. But when click it, it shows the column name and ready to put some fields. All I want is to show all the column name when visiting to the user table.

Im just a newbie in Django Framework, I read some documents and ways to extend my admin table. All I want is to add column name on existing admin table.

#models.py
from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    contact_number = models.CharField(max_length=250, blank=True)

#views.py

from django.shortcuts import render
from .models import User 
from django.urls import reverse_lazy
from django.views import generic
from .forms import NewUserCreationForm
from django.http import HttpResponseRedirect

class SignUp(generic.CreateView):
    form_class = NewUserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'signup.html'

def userView(request):
    users = User.objects.all()
    return render(request,'registration/view_users.html', 
        {"all_users":users})


#admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User
from .forms import NewUserCreationForm, NewUserChangeForm

class NewUserAdmin(UserAdmin):
    add_form = NewUserCreationForm
    form = NewUserChangeForm
    model = User
    list_display = 
    ['username']

admin.site.register(User,NewUserAdmin)

I want to show all the column on the admin site.

Upvotes: 0

Views: 1163

Answers (1)

TechSavy
TechSavy

Reputation: 1340

list_display = ['username'] will only display username field, so you need add contact_number field also, like this:

list_display = ['username', 'contact_number']

Add all the columns to list_display field which you want to display.

You could also try this for all fields:

list_display = [field.name for field in User._meta.get_fields()]

This will display list in sorted order.

Upvotes: 1

Related Questions