How to display full name in admin with (or without) get_full_name()?

Hi,

I’m trying to access to the get_full_name() method in the admin of my project. I extended the Django User model with a Bio class, connected through a “user” OneToOneField and updated via signals as exposed here. The main class of my models.py, ObjetArchi, is also related to the User model through the “auteur” ForeignKey field.

I’d like to show the users full name’s, as allowed by the get_full_name() method, in every part of the admin: on list_display, list_filter and fieldsets, as well as in the ObjetArchi form.

How and where should I write this method?

Thanks for your help!

models.py:

class Bio(models.Model):
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        null=True
        )
    coordonnees = models.CharField(
        max_length=300,
        verbose_name='Coordonnées institutionnelles de l\'auteur',
        help_text='300 caractères max.',
        blank=True
        )
    site_web = models.URLField(
        verbose_name='Site web personnel',
        blank=True
        )
    biographie = models.TextField(
        blank=True,
        null=True,
    )

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Bio.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.bio.save()

(...)

class ObjetArchi(gis_models.Model):
    id = models.AutoField(
        primary_key=True,
        verbose_name= 'Identifiant'
        )   
    titre = models.CharField(
        max_length=100,
        verbose_name='Titre',
        blank=True,
        null=True
        )
    date = models.DateField(
        default=timezone.now,
        verbose_name='Date de publication'
        )
    auteur = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        related_name='objets_archi_user',
        verbose_name='Auteur.e',
        blank=True,
        null=True
        )
    resume = models.TextField(
        verbose_name='Résumé',
        help_text='Formatage Markdown:<br>*Italique*<br>**Gras**<br>',
        blank=True,
        null=True
        )
    last_modified = models.DateTimeField(
        auto_now=True,
        verbose_name='Date de dernière modification')
    last_modified_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        verbose_name='Modifié par',
        related_name='objet_archi_modifiers',
        blank=True,
        null=True
    )

admin.py:

class BioInline(admin.StackedInline):
    model = Bio
    can_delete = False
    verbose_name = 'biographie'

class UserAdmin(BaseUserAdmin):
    inlines = (BioInline,)

class ObjetArchiAdmin(GeoModelAdminMixin, nested_admin.NestedModelAdmin):
    formfield_overrides = {
        models.TextField: {'widget': AdminMarkdownxWidget},
    }
    form = ObjetArchiForm
    list_display = ('titre', 'auteur', 'date', 'id')
    list_filter = ('auteur',)
    readonly_fields = ('auteur', 'last_modified', 'last_modified_by',)
    fieldsets = (
        ('État-Civil', {
            'fields': (('titre', 'date'), ('auteur', 'last_modified', 'last_modified_by'), 'resume')
        }),
    def save_model(self, request, obj, form, change):
        if not obj.auteur:
            obj.auteur = request.user
        obj.last_modified_by = request.user
        obj.save()

(...)

admin.site.unregister(User)
admin.site.register(User, UserAdmin)
admin.site.register(ObjetArchi, ObjetArchiAdmin)

list_display lets you pass a callable taking one parameter, the object being rendered.

list_filter provides a similar (but not identical) facility.

I’m not quite sure I understand what you mean by using it in fieldsets or in the form. Are you talking about using it as a label in those areas, as a default value, or as being editable as “full name”?

1 Like