Hello, I have a view that can create multiple instances of a model at once. The model has a unique_together constraint and I made a ModelSerializer based on it, something like
class MyAttributeChangelog(models.Model): parent = models.ForeignKey(ParentModel, on_delete=models.CASCADE) start_datetime = models.DateTimeField() ... class Meta: ordering = ['start_datetime'] unique_together = ['start_datetime', 'installation'] class MyAttributeChangelogSerializer(serializers.ModelSerializer): ... class Meta: model = MyAttributeChangelog fields = ('id', 'parent', 'start_datetime', ...) The view receives a list of dicts and uses the serializer (with many=True) to validate and save them. It works fine, except for the enforcement of the unique_together constraint. In the following test I get a IntegrityError rather than a bad request response, which was the expected: def test_update_changelog_list_with_duplicate_datetimes(self): """ ... """ parent = Parent.objects.get(...) url = f'/api/parents/{parent.id}/my_attribute_changelog/' data = [ { 'start_datetime': datetime(2018, 9, 20, 9, 30, 20, tzinfo=timezone.get_current_timezone()) ... }, { 'start_datetime': datetime(2018, 9, 20, 9, 30, 20, tzinfo=timezone.get_current_timezone()) ... } ] response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) ... I tried adding the UniqueTogetherValidator explicitly to the serializer class, despite my understanding that it would be inferred from the Model, but the error persists. I know I can rather easily validate this in the view, iterating through the items, but I think the validator should take care of that. Am I missing something? Best regards. -- You received this message because you are subscribed to the Google Groups "Django REST framework" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.