Skip to content

Commit

Permalink
Merge pull request #11 from StephanJarisch/main
Browse files Browse the repository at this point in the history
SIPWU-1187: Fix bug, listing attachments if content_type_id and object_id reference a non existing entity
  • Loading branch information
nezhar authored Aug 21, 2024
2 parents 2bfcd16 + c8bddc4 commit 9e20d67
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions drf_attachments/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,20 @@ class AttachmentAdmin(admin.ModelAdmin, AttachmentAdminMixin):
@staticmethod
def content_object(obj):
entity = obj.content_object
app_label = entity._meta.app_label
model_name = entity._meta.model_name
try:
admin_url = reverse(
f"admin:{app_label}_{model_name}_change", args=(entity.pk,)
)
return mark_safe(f'<a href="{admin_url}">{entity}</a>')
except NoReverseMatch:
return entity

if entity:
app_label = entity._meta.app_label
model_name = entity._meta.model_name

try:
admin_url = reverse(
f"admin:{app_label}_{model_name}_change", args=(entity.pk,)
)
return mark_safe(f'<a href="{admin_url}">{entity}</a>')
except NoReverseMatch:
return entity

return "not found (change object_id or content_type)"

def get_urls(self):
urls = super().get_urls()
Expand Down

0 comments on commit 9e20d67

Please sign in to comment.