Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #16433 -- Fixed a help_text/read only field interaction that ca…

…used an admin crash.

Thanks chris at cogdon.org for the report and admackin for the patch.
  • Loading branch information...
commit af953c45cc56df28d02675e196eca1b54ce28a43 1 parent cf8d6e9
@timgraham timgraham authored
View
9 django/contrib/admin/util.py
@@ -327,10 +327,15 @@ def label_for_field(name, model, model_admin=None, return_attr=False):
def help_text_for_field(name, model):
+ help_text = ""
try:
- help_text = model._meta.get_field_by_name(name)[0].help_text
+ field_data = model._meta.get_field_by_name(name)
except models.FieldDoesNotExist:
- help_text = ""
+ pass
+ else:
+ field = field_data[0]
+ if not isinstance(field, RelatedObject):
+ help_text = field.help_text
return smart_text(help_text)
View
6 tests/admin_views/admin.py
@@ -450,6 +450,10 @@ def get_changelist(self, request, **kwargs):
return CustomChangeList
+class ToppingAdmin(admin.ModelAdmin):
+ readonly_fields = ('pizzas',)
+
+
class PizzaAdmin(admin.ModelAdmin):
readonly_fields = ('toppings',)
@@ -763,7 +767,7 @@ class ChoiceList(admin.ModelAdmin):
site.register(Promo)
site.register(ChapterXtra1, ChapterXtra1Admin)
site.register(Pizza, PizzaAdmin)
-site.register(Topping)
+site.register(Topping, ToppingAdmin)
site.register(Album, AlbumAdmin)
site.register(Question)
site.register(Answer)
View
2  tests/admin_views/models.py
@@ -495,7 +495,7 @@ class Topping(models.Model):
class Pizza(models.Model):
name = models.CharField(max_length=20)
- toppings = models.ManyToManyField('Topping')
+ toppings = models.ManyToManyField('Topping', related_name='pizzas')
class Album(models.Model):
View
14 tests/admin_views/tests.py
@@ -50,7 +50,8 @@
OtherStory, ComplexSortedPerson, PluggableSearchPerson, Parent, Child, AdminOrderedField,
AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject,
- Simple, UndeletableObject, UnchangeableObject, Choice, ShortMessage, Telegram)
+ Simple, UndeletableObject, UnchangeableObject, Choice, ShortMessage,
+ Telegram, Pizza, Topping)
from .admin import site, site2
@@ -3638,6 +3639,17 @@ def test_change_form_renders_correct_null_choice_value(self):
self.assertContains(response, '<p>No opinion</p>', html=True)
self.assertNotContains(response, '<p>(None)</p>')
+ def test_readonly_backwards_ref(self):
+ """
+ Regression test for #16433 - backwards references for related objects
+ broke if the related field is read-only due to the help_text attribute
+ """
+ topping = Topping.objects.create(name='Salami')
+ pizza = Pizza.objects.create(name='Americano')
+ pizza.toppings.add(topping)
+ response = self.client.get('/test_admin/admin/admin_views/topping/add/')
+ self.assertEqual(response.status_code, 200)
+
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class RawIdFieldsTest(TestCase):
Please sign in to comment.
Something went wrong with that request. Please try again.