Ticket #16433: django-16433-with-test.patch

File django-16433-with-test.patch, 3.3 KB (added by Andy MacKinlay, 13 years ago)

Same patch as previous with unit tests added

  • django/contrib/admin/util.py

     
    304304def help_text_for_field(name, model):
    305305    try:
    306306        help_text = model._meta.get_field_by_name(name)[0].help_text
    307     except models.FieldDoesNotExist:
     307    except (models.FieldDoesNotExist, AttributeError):
    308308        help_text = ""
    309309    return smart_unicode(help_text)
    310310
  • tests/regressiontests/admin_views/admin.py

     
    411411    def get_changelist(self, request, **kwargs):
    412412        return CustomChangeList
    413413
     414class ToppingAdmin(admin.ModelAdmin):
     415    readonly_fields = ('pizzas',)
    414416
    415417class PizzaAdmin(admin.ModelAdmin):
    416418    readonly_fields = ('toppings',)
     
    630632site.register(Promo)
    631633site.register(ChapterXtra1, ChapterXtra1Admin)
    632634site.register(Pizza, PizzaAdmin)
    633 site.register(Topping)
     635site.register(Topping, ToppingAdmin)
    634636site.register(Album, AlbumAdmin)
    635637site.register(Question)
    636638site.register(Answer)
  • tests/regressiontests/admin_views/tests.py

     
    4242    FoodDelivery, RowLevelChangePermissionModel, Paper, CoverLetter, Story,
    4343    OtherStory, ComplexSortedPerson, Parent, Child, AdminOrderedField,
    4444    AdminOrderedModelMethod, AdminOrderedAdminMethod, AdminOrderedCallable,
    45     Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject)
     45    Report, MainPrepopulated, RelatedPrepopulated, UnorderedObject, Pizza, Topping)
    4646
    4747
    4848ERROR_MESSAGE = "Please enter the correct username and password \
     
    31043104        response = self.client.get('/test_admin/admin2/auth/user/%s/password/' % su.pk)
    31053105        self.assertEquals(response.status_code, 404)
    31063106
     3107    def test_readonly_backwards_ref(self):
     3108        """Regression test for #16433 - backwards references for related objects
     3109        break if the related field is read-only - due to the .help_text attribute
     3110        """
     3111        topping = Topping.objects.create(name='Salami')
     3112        pizza = Pizza.objects.create(name='Americano')
     3113        pizza.toppings.add(topping)
     3114        response = self.client.get('/test_admin/admin/admin_views/topping/add/')
     3115        self.assertEqual(response.status_code, 200)
    31073116
     3117
    31083118class RawIdFieldsTest(TestCase):
    31093119    urls = "regressiontests.admin_views.urls"
    31103120    fixtures = ['admin-views-users.xml']
  • tests/regressiontests/admin_views/models.py

     
    455455class Topping(models.Model):
    456456    name = models.CharField(max_length=20)
    457457
    458 
    459458class Pizza(models.Model):
    460459    name = models.CharField(max_length=20)
    461     toppings = models.ManyToManyField('Topping')
     460    toppings = models.ManyToManyField('Topping', related_name='pizzas')
    462461
    463462
    464463class Album(models.Model):
Back to Top