From 59e8930a41da2eec8690e3af2a37f4f40cc029a6 Mon Sep 17 00:00:00 2001 From: Adriano Date: Wed, 30 Oct 2024 14:54:33 +0100 Subject: [PATCH] update obj.id to obj.pk to accomodate different pks than the autogenerated id --- admin_toolkit/admin_mixins.py | 50 +++++++++++------------------------ 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/admin_toolkit/admin_mixins.py b/admin_toolkit/admin_mixins.py index 1bd10a3..47e1ab2 100644 --- a/admin_toolkit/admin_mixins.py +++ b/admin_toolkit/admin_mixins.py @@ -160,13 +160,9 @@ def formfield_for_dbfield(self, db_field, **kwargs): kwargs.pop("request", None) type = db_field.remote_field.__class__.__name__ if type == "ManyToOneRel": - kwargs["widget"] = VerboseForeignKeyRawIdWidget( - db_field.remote_field, site - ) + kwargs["widget"] = VerboseForeignKeyRawIdWidget(db_field.remote_field, site) elif type == "ManyToManyRel": - kwargs["widget"] = VerboseManyToManyRawIdWidget( - db_field.remote_field, site - ) + kwargs["widget"] = VerboseManyToManyRawIdWidget(db_field.remote_field, site) return db_field.formfield(**kwargs) return super().formfield_for_dbfield(db_field, **kwargs) @@ -233,10 +229,8 @@ def formfield_for_manytomany(self, db_field, request, **kwargs): class BaseAdminMixin(object): def _display_image(self, img_field, width=80, height=80, show_link=True): - html_tag = ( - "".format( - img_field=img_field, width=width, height=height - ) + html_tag = "".format( + img_field=img_field, width=width, height=height ) if show_link: html_tag = "{img_tag}".format( @@ -245,9 +239,7 @@ def _display_image(self, img_field, width=80, height=80, show_link=True): return html_tag def _display_datetime(self, datetime_field): - return formats.date_format( - localtime(datetime_field, get_current_timezone()), "SHORT_DATETIME_FORMAT" - ) + return formats.date_format(localtime(datetime_field, get_current_timezone()), "SHORT_DATETIME_FORMAT") def _display_date(self, date_field): return formats.date_format(date_field, "SHORT_DATE_FORMAT") @@ -278,9 +270,7 @@ def display_user(self, obj): return "-" display_user.short_description = _("User") """ - return '{1}' "".format( - fk_field.admin_change_url, fk_field - ) + return '{1}' "".format(fk_field.admin_change_url, fk_field) def _display_m2m_objects(self, obj, m2m_field_name, label="elements"): """ @@ -304,9 +294,7 @@ def display_users(self, obj): """ return self._display_related_objects(obj, m2m_field_name, label) - def _display_related_objects( - self, obj, related_field_name, label="elements", show_generic_link=True - ): + def _display_related_objects(self, obj, related_field_name, label="elements", show_generic_link=True): """ Use this method in your admin to display your 1..N or N..N objects (from related model) filtered changelist's link. set show_generic_link to false to show a list of all related objects links @@ -338,23 +326,18 @@ def display_devices(self, obj): related_name = related_manager.field.name related_model = related_manager.model if show_generic_link: - url = "{}?{}__id__exact={}".format( - related_model.admin_changelist_url(), related_name, obj.id - ) + url = "{}?{}__id__exact={}".format(related_model.admin_changelist_url(), related_name, obj.pk) elements_count = related_manager.count() - return ( - 'Display {elements_count} {label}' - "".format(url=url, elements_count=elements_count, label=label) + return 'Display {elements_count} {label}' "".format( + url=url, elements_count=elements_count, label=label ) else: return "
".join( - f"""{related_obj.__str__()}""" + f"""{related_obj.__str__()}""" for related_obj in related_manager.all() ) - def _display_generic_related_objects( - self, obj, related_field_name, label="elements" - ): + def _display_generic_related_objects(self, obj, related_field_name, label="elements"): """ Use this method in your admin to display your 1..N or N..N objects (from related model) filtered changelist's link. @@ -396,9 +379,8 @@ def display_tags(self, obj): obj.id, ) elements_count = related_manager.count() - return ( - 'Display {elements_count} {label}' - "".format(url=url, elements_count=elements_count, label=label) + return 'Display {elements_count} {label}' "".format( + url=url, elements_count=elements_count, label=label ) def _display_related_objects_repr( @@ -430,6 +412,4 @@ def display_devices(self, obj): display_devices.short_description = _("Devices") """ related_manager = getattr(obj, related_field_name) - return separator.join( - related_obj.__repr__() for related_obj in related_manager.all() - ) + return separator.join(related_obj.__repr__() for related_obj in related_manager.all())