Skip to content

Page 203 - Chapter 4: Issue with account/admin.py #36

@alphaveneno

Description

@alphaveneno

The author creates a custom user model by extending the default User model. However the admin.py code for registering this custom user model is incorrect. The original code does not add 'Date of Birth' or 'Photo' fields to the user model; these fields are not present in localhost:8000/admin/users/ .

Original code:


from django.contrib import admin

from .models import Profile


@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
    list_display = ['user', 'date_of_birth', 'photo']
    raw_id_fields = ['user']

My revision:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User

from .models import Profile

class ProfileInline(admin.StackedInline):
    model = Profile
    can_delete = False
    verbose_name_plural = "profile"
    list_display = ['user', 'date_of_birth', 'photo']
    raw_id_fields = ['user']

class UserAdmin(BaseUserAdmin):
    inlines = [ProfileInline]

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

A section called "PROFILE" should appear at the bottom of the page for each user, with a form for adding 'Date of Birth' and uploading 'Photo'

Reference: https://docs.djangoproject.com/en/5.1/topics/auth/customizing/#extending-the-existing-user-model

Note: this is also an issue in the previous edition.


Corollary:

It is advisable to have a default image available to be displayed if the intended image fails to render.

re: account/user/list.html code on pg. 293 (Chapter7):

I replaced this line:

<img src="{% thumbnail user.profile.photo 180x180 %}">

with this line:

<img src="{% thumbnail user.profile.photo|default:'pug.jpg' 180x180 %}" class="user-detail">

where 'pug.jpg' is an image file inside the media folder next to the images and users folders.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions