Ticket #13696: 13696.diff

File 13696.diff, 5.5 KB (added by dArignac, 4 years ago)

the patch for the issue

  • tests/regressiontests/admin_inlines/tests.py

     
    44
    55# local test models
    66from models import (Holder, Inner, InnerInline, Holder2, Inner2, Holder3,
    7     Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child)
     7    Inner3, Person, OutfitItem, Fashionista, Teacher, Parent, Child, Author2,
     8    Book2)
    89
    910
    1011class TestInline(TestCase):
     
    102103        # The right callabe should be used for the inline readonly_fields
    103104        # column cells
    104105        self.assertContains(response, '<p>Callable in QuestionInline</p>')
     106       
     107    def test_inline_ids_for_custom_pks(self):
     108        """
     109        Tests if the hidden input fields for inline element ids are rendered
     110        if the inline model has a custom and non-editable primary key.
     111        Refs #13696
     112        """
     113        # create the sample objects
     114        author = Author2.objects.create(name='Terry Goodkind')
     115        book = Book2.objects.create(title='Faith of the Fallen', author=author)
     116        # navigate to the author creation page
     117        response = self.client.get('/test_admin/admin/admin_inlines/author2/add/')
     118        self.assertEqual(response.status_code, 200)
     119        # assure the inline models of book got a hidden field with id and name
     120        # the first stacked inline
     121        self.assertRegexpMatches(response.content, r'<input .*(name="book2_set-0-id").*')
     122        self.assertRegexpMatches(response.content, r'<input .*(id="id_book2_set-0-id").*')
     123        # the second tabular inline
     124        self.assertRegexpMatches(response.content, r'<input .*(name="book2_set-2-0-id").*')
     125        self.assertRegexpMatches(response.content, r'<input .*(id="id_book2_set-2-0-id").*')
     126        # navigate to the author admin site for the created author instance
     127        response = self.client.get('/test_admin/admin/admin_inlines/author2/%s/' % author.pk)
     128        self.assertEqual(response.status_code, 200)
     129        # assure the inline models of book got a hidden field with id and name
     130        # the first stacked inline
     131        self.assertRegexpMatches(response.content, r'<input .*(name="book2_set-0-id").*')
     132        self.assertRegexpMatches(response.content, r'<input .*(id="id_book2_set-0-id").*')
     133        # the second tabular inline
     134        self.assertRegexpMatches(response.content, r'<input .*(name="book2_set-2-0-id").*')
     135        self.assertRegexpMatches(response.content, r'<input .*(id="id_book2_set-2-0-id").*')
     136       
    105137
    106138class TestInlineMedia(TestCase):
    107139    fixtures = ['admin-views-users.xml']
  • tests/regressiontests/admin_inlines/models.py

     
    191191
    192192admin.site.register(Poll, PollAdmin)
    193193admin.site.register(Novel, NovelAdmin)
     194
     195
     196# models for #13696
     197class Author2(models.Model):
     198    name = models.CharField(max_length=100)
     199   
     200class Book2(models.Model):
     201    id = models.CharField(max_length=32, primary_key=True, editable=False)
     202    author = models.ForeignKey(Author2)
     203    title = models.CharField(max_length=100)
     204
     205    def save(self, *args, **kwargs):
     206        """
     207        Creates a new random id and assigns it to the primary field.
     208        """
     209        if not self.id:
     210            import random
     211            self.id = str(random.random())
     212        super(Book2, self).save(*args, **kwargs)
     213       
     214class Book2StackedInline(admin.StackedInline):
     215    model = Book2
     216   
     217class Book2TabularInline(admin.TabularInline):
     218    model = Book2
     219
     220class Author2Admin(admin.ModelAdmin):
     221    inlines = [Book2StackedInline, Book2TabularInline]
     222    class Meta:
     223        model = Author2
     224
     225admin.site.register(Author2, Author2Admin)
  • django/contrib/admin/templates/admin/edit_inline/stacked.html

     
    1313  {% for fieldset in inline_admin_form %}
    1414    {% include "admin/includes/fieldset.html" %}
    1515  {% endfor %}
    16   {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
     16  {% if inline_admin_form.pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
    1717  {{ inline_admin_form.fk_field.field }}
    1818</div>{% endfor %}
    1919</div>
  • django/contrib/admin/templates/admin/edit_inline/tabular.html

     
    2727          {% if inline_admin_form.original %} {{ inline_admin_form.original }}{% endif %}
    2828          {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
    2929            </p>{% endif %}
    30           {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
     30          {% if inline_admin_form.pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
    3131          {{ inline_admin_form.fk_field.field }}
    3232          {% spaceless %}
    3333          {% for fieldset in inline_admin_form %}
Back to Top