-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Support violation_error_code and violation_error_message from UniqueConstraint in UniqueTogetherValidator #9766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -616,6 +616,26 @@ class Meta: | |||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
class UniqueConstraintCustomMessageCodeModel(models.Model): | ||||||||||||||||||||||||||||||||||
username = models.CharField(max_length=32) | ||||||||||||||||||||||||||||||||||
company_id = models.IntegerField() | ||||||||||||||||||||||||||||||||||
role = models.CharField(max_length=32) | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
class Meta: | ||||||||||||||||||||||||||||||||||
constraints = [ | ||||||||||||||||||||||||||||||||||
models.UniqueConstraint( | ||||||||||||||||||||||||||||||||||
fields=("username", "company_id"), | ||||||||||||||||||||||||||||||||||
name="unique_username_company_custom_msg", | ||||||||||||||||||||||||||||||||||
violation_error_message="Username must be unique within a company.", | ||||||||||||||||||||||||||||||||||
**(dict(violation_error_code="duplicate_username") if django_version[0] >= 5 else {}), | ||||||||||||||||||||||||||||||||||
Comment on lines
+625
to
+630
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] The inline conditional dictionary unpacking makes the code harder to read. Consider extracting this into a separate variable or using a cleaner approach for version-dependent constraint arguments.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and this too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it’s not possible to specify All possible Meta options are listed in the documentation: |
||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||
models.UniqueConstraint( | ||||||||||||||||||||||||||||||||||
fields=("company_id", "role"), | ||||||||||||||||||||||||||||||||||
name="unique_company_role_default_msg", | ||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
class UniqueConstraintSerializer(serializers.ModelSerializer): | ||||||||||||||||||||||||||||||||||
class Meta: | ||||||||||||||||||||||||||||||||||
model = UniqueConstraintModel | ||||||||||||||||||||||||||||||||||
|
@@ -628,6 +648,12 @@ class Meta: | |||||||||||||||||||||||||||||||||
fields = ('title', 'age', 'tag') | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
class UniqueConstraintCustomMessageCodeSerializer(serializers.ModelSerializer): | ||||||||||||||||||||||||||||||||||
class Meta: | ||||||||||||||||||||||||||||||||||
model = UniqueConstraintCustomMessageCodeModel | ||||||||||||||||||||||||||||||||||
fields = ('username', 'company_id', 'role') | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
class TestUniqueConstraintValidation(TestCase): | ||||||||||||||||||||||||||||||||||
def setUp(self): | ||||||||||||||||||||||||||||||||||
self.instance = UniqueConstraintModel.objects.create( | ||||||||||||||||||||||||||||||||||
|
@@ -778,6 +804,31 @@ class Meta: | |||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||
assert serializer.is_valid() | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
def test_unique_constraint_custom_message_code(self): | ||||||||||||||||||||||||||||||||||
UniqueConstraintCustomMessageCodeModel.objects.create(username="Alice", company_id=1, role="member") | ||||||||||||||||||||||||||||||||||
expected_code = "duplicate_username" if django_version[0] >= 5 else UniqueTogetherValidator.code | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
serializer = UniqueConstraintCustomMessageCodeSerializer(data={ | ||||||||||||||||||||||||||||||||||
"username": "Alice", | ||||||||||||||||||||||||||||||||||
"company_id": 1, | ||||||||||||||||||||||||||||||||||
"role": "admin", | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
assert not serializer.is_valid() | ||||||||||||||||||||||||||||||||||
assert serializer.errors == {"non_field_errors": ["Username must be unique within a company."]} | ||||||||||||||||||||||||||||||||||
assert serializer.errors["non_field_errors"][0].code == expected_code | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
def test_unique_constraint_default_message_code(self): | ||||||||||||||||||||||||||||||||||
UniqueConstraintCustomMessageCodeModel.objects.create(username="Alice", company_id=1, role="member") | ||||||||||||||||||||||||||||||||||
serializer = UniqueConstraintCustomMessageCodeSerializer(data={ | ||||||||||||||||||||||||||||||||||
"username": "John", | ||||||||||||||||||||||||||||||||||
"company_id": 1, | ||||||||||||||||||||||||||||||||||
"role": "member", | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
expected_message = UniqueTogetherValidator.message.format(field_names=', '.join(("company_id", "role"))) | ||||||||||||||||||||||||||||||||||
assert not serializer.is_valid() | ||||||||||||||||||||||||||||||||||
assert serializer.errors == {"non_field_errors": [expected_message]} | ||||||||||||||||||||||||||||||||||
assert serializer.errors["non_field_errors"][0].code == UniqueTogetherValidator.code | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
# Tests for `UniqueForDateValidator` | ||||||||||||||||||||||||||||||||||
# ---------------------------------- | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dictionary comprehension will overwrite constraints with identical field combinations from different model classes. This could result in losing custom error messages/codes if a parent class constraint is overwritten by a child class constraint with the same fields but different error configuration.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@s-aleshin can you please cross check this suggestion?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi! As mentioned in an earlier comment, iterating over parent models here might not be necessary — constraints are usually defined per model and not inherited unless explicitly copied.
That said, if we keep the current logic, we should switch the order in line 1611:
Unfortunately, I can’t fix it right now — currently on vacation without a laptop. Will be able to fix in a week.