Skip to content

Commit

Permalink
Add a constraint in the DB model as well.
Browse files Browse the repository at this point in the history
This really should've been handled automatically by DRF, and in the
future, it will be. But for now, we need to have constraints both on the
serializer (to get status code 400), and on the model (to prevent direct
database constraint violations).

See encode/django-rest-framework#7173
  • Loading branch information
Leon Sandøy committed Jul 29, 2020
1 parent f15a345 commit 12f447b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ class Migration(migrations.Migration):
('comment', models.TextField(help_text="Optional comment on this entry.", null=True)),
],
bases=(pydis_site.apps.api.models.mixins.ModelReprMixin, models.Model),
),
migrations.AddConstraint(
model_name='filterlist',
constraint=models.UniqueConstraint(fields=('content', 'type'), name='unique_filter_list')
)
]
11 changes: 11 additions & 0 deletions pydis_site/apps/api/models/bot/filter_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,14 @@ class FilterList(ModelTimestampMixin, ModelReprMixin, models.Model):
help_text="Optional comment on this entry.",
null=True
)

class Meta:
"""Metaconfig for this model."""

# This constraint ensures only one filterlist with the
# same content can exist. This means that we cannot have both an allow
# and a deny for the same item, and we cannot have duplicates of the
# same item.
constraints = [
models.UniqueConstraint(fields=['content', 'type'], name='unique_filter_list'),
]

0 comments on commit 12f447b

Please sign in to comment.