diff options
author | 2022-03-22 18:55:02 +0100 | |
---|---|---|
committer | 2022-03-22 18:55:08 +0100 | |
commit | 3ff628ad44b80f1c5483832f72ee8b63bcbc4fdb (patch) | |
tree | 3cf92416dbbc088725520be04a1e813ba8666a10 /pydis_site/apps/api/models/bot | |
parent | Sync Filter models with relating migrations, adjust code consistency (diff) |
Add UniqueConstraint to the Filter model
- The UniqueConstraint includes every field, except for id and description.
Diffstat (limited to 'pydis_site/apps/api/models/bot')
-rw-r--r-- | pydis_site/apps/api/models/bot/filters.py | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/pydis_site/apps/api/models/bot/filters.py b/pydis_site/apps/api/models/bot/filters.py index f8bbfd14..708ceadc 100644 --- a/pydis_site/apps/api/models/bot/filters.py +++ b/pydis_site/apps/api/models/bot/filters.py @@ -113,7 +113,7 @@ class FilterList(FilterSettingsMixin): return f"Filter {FilterListType(self.list_type).label}list {self.name!r}" -class Filter(FilterSettingsMixin): +class FilterBase(FilterSettingsMixin): """One specific trigger of a list.""" content = models.CharField(max_length=100, help_text="The definition of this filter.") @@ -173,3 +173,29 @@ class Filter(FilterSettingsMixin): def __str__(self) -> str: return f"Filter {self.content!r}" + + class Meta: + """Metaclass for FilterBase to make it abstract model.""" + + abstract = True + + +class Filter(FilterBase): + """ + The main Filter models based on `FilterBase`. + + The purpose to have this model is to have access to the Fields of the Filter model + and set the unique constraint based on those fields. + """ + + class Meta: + """Metaclass Filter to set the unique constraint.""" + + constraints = ( + UniqueConstraint( + fields=tuple( + [field.name for field in FilterBase._meta.fields + if field.name != "id" and field.name != "description"] + ), + name="unique_filters"), + ) |