#How to show User FirstName LastName (UserName)

73 messages · Page 1 of 1 (latest)

gritty ravine
#

Hi. I can't find any answer... I want to see User's first name, last name and username in admin display list.

For example

Python Django (pythondjango)
Mustafa Deniz (mustafadeniz)
Albert A (alba)

Ok, how can show this format? Thnks.

ruby wadi
#

Override dunder repr method in your model: python class YourModel(models.Model): first_name = models.CharField(max_length=16) last_name = models.CharField(max_length=16) ... def __repr__(self): return f"{self.first_name} {self.last_name}"

merry jasper
#

You can manipulate the __str__ function, for example:

In your User model:

class User(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    username = models.CharField(max_length=100)

    def __str__(self):
        return f"{self.first_name} {self.last_name} ({self.username})"
ruby wadi
#

repr is used for objects internal representation, str is used for human readable format

gritty ravine
#

These not true answer.

merry jasper
ruby wadi
#

Yes, Admin uses repr to show the objects

merry jasper
ruby wadi
#

My bad, Admin uses str for display

merry jasper
gritty ravine
#

For exampla

class pool(models.Model)
    question = models.CharField(max_length=100)
    creator = models.ForeignKey(User, on_delete=models.CASCADE)

How to show user name format?

ruby wadi
#
A few special cases to note about list_display:

If the field is a ForeignKey, Django will display the __str__() of the related object.

ManyToManyField fields aren’t supported, because that would entail executing a separate SQL statement for each row in the table. If you want to do this nonetheless, give your model a custom method, and add that method’s name to list_display. (See below for more on custom methods in list_display.)

If the field is a BooleanField, Django will display a pretty “yes”, “no”, or “unknown” icon instead of True, False, or None.

If the string given is a method of the model, ModelAdmin or a callable, Django will HTML-escape the output by default. To escape user input and allow your own unescaped tags, use format_html().
gritty ravine
#

Check your project. Look history of any model You will see format.

atomic iris
merry jasper
ruby wadi
gritty ravine
#

in Admin Display List

atomic iris
gritty ravine
atomic iris
gritty ravine
atomic iris
gritty ravine
#

Why am I creating a custom user model? Oh patience!

#

Listen!

#

Open one project .

#

Edit something in your app.

#

Check history.

#

You will see (username) First name, Last name

#

Now... Why am I crating custom... ?

merry jasper
#

we are trying to help

gritty ravine
#

🙂

atomic iris
#

Ahh why

During the lifecycle of development and the runtime you'll build more and more things that relate to the user model

As time goes on you will probably find something about the default user model you want to change

Now you'll have to swap out an existing user model for your new custom one

That means migrating EVERY SINGLE relation to your old model, possibly dropping constraints to make that possible, recreating a lot of constraints and then finally deleting the default user model from the database

If you swap out the user model at the beginning of a project that entire ordeal gets a whole lot easier as django's migration system does most of the heavy lifting for you

ruby wadi
ruby wadi
gritty ravine
atomic iris
merry jasper
#
from django.contrib import admin
from .models import User

class UserAdmin(admin.ModelAdmin):
    list_display = ('full_name', 'username')

    def full_name(self, obj):
        return f"{obj.first_name} {obj.last_name}"
    full_name.short_description = 'Full Name'  # Sets the column header

admin.site.register(User, UserAdmin)
ruby wadi
atomic iris
#

So if i've got a foreignkey from my user model which points to lets say a house, and the field is called "house", and every house has an address my list_display would include "house__name" for displaying the house name besides the user

gritty ravine
atomic iris
gritty ravine
atomic iris
#

Then you can take exactly what RB sent. A custom modeladmin with a method which returns the appropriate string. (Thats also in the docs that i've sent you)

In any case i'd suggest doing the official django tutorial, it covers stuff like this
https://docs.djangoproject.com/en/5.0/intro/tutorial01/

merry jasper
gritty ravine
#

list_display = ('full_name', 'username') ????????????????????????????

#

My model has not these fields.

atomic iris
merry jasper
#

Bro send a picture

ruby wadi
# gritty ravine Not worked. Until only username.

I think you're confusing us. In your admin panel you'd have a User model (Django's inbuilt) and Pool model. Now, do you want to see the creator fields (first_name, last_name and username) for Pool objects or for User objects? Cause if it's latter, you need to extend UserAdmin from BaseUserAdmin and do what I did earlier. If it's for Pool model, PoolAdmin should work with some tweaks based your actual code.

merry jasper
#

oh, you can't

gritty ravine
gritty ravine
ruby wadi
#

Bruh, you need to tell us for which model do you want to see those fields?

merry jasper
#

List display are broken?

ruby wadi
#

User or Pool?

merry jasper
gritty ravine
#

Come on CHAT #856567261900832812

merry jasper
#

Of course

gritty ravine
#

writeeeeeeeeeeeeee

merry jasper
#

in the 15 line of the code

#

replace the str return

#

for you expected output

ruby wadi
#

In your list_display add "upper_case_name"

merry jasper
#

return f'{self.first_name} {self.last_name} ({self.username})'

gritty ravine
#

class YourModelAdmin(admin.ModelAdmin):
list_display = ('your_field', 'username', 'first_name', 'last_name')

def username(self, obj):
    return obj.user.username

def first_name(self, obj):
    return obj.user.first_name

def last_name(self, obj):
    return obj.user.last_name

admin.site.register(YourModel, YourModelAdmin)

#

DONE!

Solution:

admin.py

@admin.register(YourAppsModel)
class YourAppsModelAdmin(admin.ModelAdmin):
    readonly_fields = ('creator', 'create_at')
    list_display = ["YourAppsModelField", "myCreatorFormat",]

    def myCreatorFormat(self, obj):
        return f'{obj.creator.first_name} {obj.creator.last_name} ({obj.creator.username})' 
merry jasper
#

Yes i helped my first friend

#

And in call