Skip to content

Commit

Permalink
Added picture profile in admin section, declaring the field as a read…
Browse files Browse the repository at this point in the history
…only
  • Loading branch information
Fer-Bar committed May 23, 2024
1 parent 6ea8408 commit c0f36f3
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion pet/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.contrib import admin
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _

from pet.filters import HasOwnerFilter
from pet.models import Breed, Pet
Expand All @@ -8,9 +10,33 @@
class PetAdmin(admin.ModelAdmin):
search_fields = ["name", "owner"]
list_display = ("name", "breed", "gender", "age", "owner", "has_owner", "is_neutered")
readonly_fields = ("age", )
readonly_fields = ("age", "picture_display",)
list_filter = (HasOwnerFilter,)

def picture_display(self, obj=None):
return mark_safe(
f'<img style="object-fit: cover;" src="{obj.picture_url}" height=250 width=100%>'
)

picture_display.short_description = _("Profile Picture")

def get_fieldsets(self, request, obj=None):
if obj is None or not obj.picture_url:
picture_fields = [
("picture",),
]
else:
picture_fields = [
("picture_display", "picture"),
]
return (
(
None, {
"fields": picture_fields + [("name", "breed", "gender", "age"), ]
}
),
)


@admin.register(Breed)
class BreedAdmin(admin.ModelAdmin):
Expand All @@ -19,4 +45,5 @@ class BreedAdmin(admin.ModelAdmin):

class PetInline(admin.TabularInline):
model = Pet
fields = ("name", "breed", "gender")
extra = 0

0 comments on commit c0f36f3

Please sign in to comment.