diff options
author | 2020-07-29 19:07:03 +0200 | |
---|---|---|
committer | 2020-07-29 19:07:03 +0200 | |
commit | 12f447bc9636382a623e6b30ae43ccc036c488b5 (patch) | |
tree | 9e96bf98d684dcb91c359e66b882fdeee6bef4d6 /pydis_site | |
parent | Fix multiple leafs for migration graph. (diff) |
Add a constraint in the DB model as well.
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 https://github.com/encode/django-rest-framework/issues/7173
Diffstat (limited to 'pydis_site')
-rw-r--r-- | pydis_site/apps/api/migrations/0058_create_new_filterlist_model.py | 4 | ||||
-rw-r--r-- | pydis_site/apps/api/models/bot/filter_list.py | 11 |
2 files changed, 15 insertions, 0 deletions
diff --git a/pydis_site/apps/api/migrations/0058_create_new_filterlist_model.py b/pydis_site/apps/api/migrations/0058_create_new_filterlist_model.py index fffb8159..aecfdad7 100644 --- a/pydis_site/apps/api/migrations/0058_create_new_filterlist_model.py +++ b/pydis_site/apps/api/migrations/0058_create_new_filterlist_model.py @@ -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') ) ] diff --git a/pydis_site/apps/api/models/bot/filter_list.py b/pydis_site/apps/api/models/bot/filter_list.py index 5961aed7..d279e137 100644 --- a/pydis_site/apps/api/models/bot/filter_list.py +++ b/pydis_site/apps/api/models/bot/filter_list.py @@ -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'), + ] |