Ticket #13091: 13091_partial_unique_together.2.diff

File 13091_partial_unique_together.2.diff, 4.1 KB (added by julien, 5 years ago)
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 912bdf3..0c8c5df 100644
    a b class BaseModelForm(BaseForm): 
    342342        Calls the instance's validate_unique() method and updates the form's
    343343        validation errors if any were raised.
    344344        """
    345         exclude = self._get_validation_exclusions()
    346345        try:
    347             self.instance.validate_unique(exclude=exclude)
     346            self.instance.validate_unique()
    348347        except ValidationError, e:
    349348            self._update_errors(e.message_dict)
    350349
  • tests/regressiontests/admin_views/models.py

    diff --git a/tests/regressiontests/admin_views/models.py b/tests/regressiontests/admin_views/models.py
    index 7d6cab3..80ff365 100644
    a b class FoodDeliveryAdmin(admin.ModelAdmin): 
    704704    list_display=('reference', 'driver', 'restaurant')
    705705    list_editable = ('driver', 'restaurant')
    706706
     707class UniqueTogether(models.Model):
     708    t1 = models.CharField(max_length=255, blank=True, null=True)
     709    t2 = models.CharField(max_length=255, blank=True, null=True)
     710    t3 = models.CharField(max_length=255, blank=True, null=True)     
     711   
     712    class Meta:
     713        unique_together = ['t1','t2','t3']
     714                 
     715class UniqueTogetherAdmin(admin.ModelAdmin):
     716    list_display = ['t3', 't1', 't2']
     717    list_editable = ['t1', 't2',]
     718   
    707719
    708720admin.site.register(Article, ArticleAdmin)
    709721admin.site.register(CustomArticle, CustomArticleAdmin)
    admin.site.register(WorkHour, WorkHourAdmin) 
    743755admin.site.register(Reservation)
    744756admin.site.register(FoodDelivery, FoodDeliveryAdmin)
    745757admin.site.register(RowLevelChangePermissionModel, RowLevelChangePermissionModelAdmin)
     758admin.site.register(UniqueTogether, UniqueTogetherAdmin)
    746759
    747760# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    748761# That way we cover all four cases:
  • tests/regressiontests/admin_views/tests.py

    diff --git a/tests/regressiontests/admin_views/tests.py b/tests/regressiontests/admin_views/tests.py
    index 642186e..da04137 100644
    a b from models import (Article, BarAccount, CustomArticle, EmptyModel, 
    3636    Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
    3737    Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee,
    3838    Question, Answer, Inquisition, Actor, FoodDelivery,
    39     RowLevelChangePermissionModel)
     39    RowLevelChangePermissionModel, UniqueTogether)
    4040
    4141
    4242class AdminViewBasicTest(TestCase):
    class AdminViewListEditable(TestCase): 
    13731373        # 1 select per object = 3 selects
    13741374        self.assertEqual(response.content.count("<select"), 4)
    13751375
     1376    def test_partial_unique_together(self):
     1377        """ Ensure that no IntegrityError is raised when editing some (but
     1378            not all) of the values specified as unique_together. Refs #13091.
     1379        """
     1380        UniqueTogether.objects.create(t1='a', t2='b', t3='c')
     1381        UniqueTogether.objects.create(t1='b', t2='b', t3='a')
     1382        UniqueTogether.objects.create(t1='c', t2='a', t3='c')
     1383        data = {
     1384            "form-TOTAL_FORMS": "3",
     1385            "form-INITIAL_FORMS": "3",
     1386            "form-MAX_NUM_FORMS": "0",
     1387
     1388            "form-0-t1": "a",
     1389            "form-0-t2": "b",
     1390            "form-0-t3": "c",
     1391            "form-0-id": "1",
     1392
     1393            "form-1-t1": "a",
     1394            "form-1-t2": "b",
     1395            "form-1-t3": "c",
     1396            "form-1-id": "2",
     1397
     1398            "form-2-t1": "a",
     1399            "form-2-t2": "b",
     1400            "form-2-t3": "c",
     1401            "form-2-id": "3",
     1402
     1403            "_save": "Save",
     1404        }
     1405        response = self.client.post('/test_admin/admin/admin_views/uniquetogether/', data)
     1406        self.assertContains(response, 'Please correct the errors below.')
     1407        self.assertContains(response, 'Unique together with this T1, T2 and T3 already exists.')
     1408       
    13761409    def test_post_messages(self):
    13771410        # Ticket 12707: Saving inline editable should not show admin
    13781411        # action warnings
Back to Top