Skip to content

Commit

Permalink
Use object instance when getting _meta info in admin
Browse files Browse the repository at this point in the history
Make sure to use the object instance in admin.py in order to
get the correct app_label and model_name. This is important
when a subclass (e.g. of Newsletter) is used.
  • Loading branch information
frennkie committed Sep 22, 2020
1 parent 57ac58d commit 94c93ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions newsletter/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class NewsletterAdmin(admin.ModelAdmin):
""" List extensions """
def _admin_url(self, obj, model, text):
url = reverse('admin:%s_%s_changelist' %
(model._meta.app_label, model._meta.model_name),
(obj._meta.app_label, obj._meta.model_name),
current_app=self.admin_site.name)

return format_html(
Expand All @@ -90,7 +90,7 @@ def admin_submissions(self, obj):

class NewsletterAdminLinkMixin(object):
def admin_newsletter(self, obj):
opts = Newsletter._meta
opts = obj._meta
newsletter = obj.newsletter
url = reverse('admin:%s_%s_change' % (opts.app_label, opts.model_name),
args=(newsletter.id,), current_app=self.admin_site.name)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.test import TestCase

from newsletter.models import Newsletter


class CustomNewsletter(Newsletter):
class Meta:
app_label = 'name_of_my_app'


class ModelTestCase(TestCase):
""" Test case for models. """

def test_newsletter_label_name(self):
""" Test that _meta returns correct app_label and model_name. """
self.assertEqual(Newsletter._meta.app_label, 'newsletter')
self.assertEqual(Newsletter._meta.model_name, 'newsletter')

obj = Newsletter()
self.assertEqual(obj._meta.app_label, 'newsletter')
self.assertEqual(obj._meta.model_name, 'newsletter')

custom_obj = CustomNewsletter()
self.assertEqual(custom_obj._meta.app_label, 'name_of_my_app')
self.assertEqual(custom_obj._meta.model_name, 'customnewsletter')

0 comments on commit 94c93ea

Please sign in to comment.