Ticket #13091: 13091_partial_unique_together.diff

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

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 2a3f8bd..09b46b5 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 60319ea..36579e2 100644
    a b class Reservation(models.Model): 
    670670    start_date = models.DateTimeField()
    671671    price = models.IntegerField()
    672672   
     673class UniqueTogether(models.Model):
     674    t1 = models.CharField(max_length=255, blank=True, null=True)
     675    t2 = models.CharField(max_length=255, blank=True, null=True)
     676    t3 = models.CharField(max_length=255, blank=True, null=True)
     677   
     678    class Meta:
     679        unique_together = ['t1','t2','t3']
     680       
     681class UniqueTogetherAdmin(admin.ModelAdmin):
     682    list_display = ['t3', 't1', 't2']
     683    list_editable = ['t1', 't2',]
     684   
    673685admin.site.register(Article, ArticleAdmin)
    674686admin.site.register(CustomArticle, CustomArticleAdmin)
    675687admin.site.register(Section, save_as=True, inlines=[ArticleInline])
    admin.site.register(CyclicOne) 
    706718admin.site.register(CyclicTwo)
    707719admin.site.register(WorkHour, WorkHourAdmin)
    708720admin.site.register(Reservation)
     721admin.site.register(UniqueTogether, UniqueTogetherAdmin)
    709722
    710723# We intentionally register Promo and ChapterXtra1 but not Chapter nor ChapterXtra2.
    711724# 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 acbbbfc..250af13 100644
    a b from models import (Article, BarAccount, CustomArticle, EmptyModel, 
    3535    Person, Persona, Picture, Podcast, Section, Subscriber, Vodcast,
    3636    Language, Collector, Widget, Grommet, DooHickey, FancyDoodad, Whatsit,
    3737    Category, Post, Plot, FunkyTag, Chapter, Book, Promo, WorkHour, Employee,
    38     Question, Answer, Inquisition, Actor)
     38    Question, Answer, Inquisition, Actor, UniqueTogether)
    3939
    4040
    4141class AdminViewBasicTest(TestCase):
    class AdminViewListEditable(TestCase): 
    13381338        # 1 select per object = 3 selects
    13391339        self.assertEqual(response.content.count("<select"), 4)
    13401340
     1341    def test_partial_unique_together(self):
     1342        """ Ensure that no IntegrityError is raised when editing some (but
     1343            not all) of the values specified as unique_together. Refs #13091.
     1344        """
     1345        UniqueTogether.objects.create(t1='a', t2='b', t3='c')
     1346        UniqueTogether.objects.create(t1='b', t2='b', t3='a')
     1347        UniqueTogether.objects.create(t1='c', t2='a', t3='c')
     1348        data = {
     1349            "form-TOTAL_FORMS": "3",
     1350            "form-INITIAL_FORMS": "3",
     1351            "form-MAX_NUM_FORMS": "0",
     1352
     1353            "form-0-t1": "a",
     1354            "form-0-t2": "b",
     1355            "form-0-t3": "c",
     1356            "form-0-id": "1",
     1357
     1358            "form-1-t1": "a",
     1359            "form-1-t2": "b",
     1360            "form-1-t3": "c",
     1361            "form-1-id": "2",
     1362
     1363            "form-2-t1": "a",
     1364            "form-2-t2": "b",
     1365            "form-2-t3": "c",
     1366            "form-2-id": "3",
     1367
     1368            "_save": "Save",
     1369        }
     1370        response = self.client.post('/test_admin/admin/admin_views/uniquetogether/', data)
     1371        self.assertContains(response, 'Please correct the errors below.')
     1372       
    13411373    def test_post_messages(self):
    13421374        # Ticket 12707: Saving inline editable should not show admin
    13431375        # action warnings
Back to Top