forked from sorl/django-mockups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraints.py
35 lines (29 loc) · 1.21 KB
/
constraints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
class InvalidConstraint(Exception):
def __init__(self, fields, *args, **kwargs):
self.fields = fields
super(InvalidConstraint, self).__init__(*args, **kwargs)
def unique_constraint(model, instance):
error_fields = []
for field in instance._meta.fields:
if field.unique:
check = {field.name: getattr(instance, field.name)}
unique = not bool(model._default_manager.filter(**check))
if not unique:
error_fields.append(field)
if error_fields:
raise InvalidConstraint(error_fields)
def unique_together_constraint(model, instance):
error_fields = []
if instance._meta.unique_together:
for unique_fields in instance._meta.unique_together:
check = {}
for field_name in unique_fields:
check[field_name] = getattr(instance, field_name)
unique = not bool(model._default_manager.filter(**check))
if not unique:
error_fields.extend(
[instance._meta.get_field_by_name(field_name)[0]
for field_name in unique_fields])
if error_fields:
raise InvalidConstraint(error_fields)