Skip to content

Commit e203a46

Browse files
committed
Adding example of validating foreign key relations.
1 parent 1e5c1c5 commit e203a46

File tree

5 files changed

+27
-10
lines changed

5 files changed

+27
-10
lines changed

README.markdown

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ the customer_contact_persons company and email as additional details.
1515
In "models.py":
1616

1717
```python
18-
from django.db import models as m
19-
2018
"""
2119
No changes to the models are needed to use flexselect.
2220
"""
@@ -46,6 +44,15 @@ class Case(m.Model):
4644
client = m.ForeignKey(Client)
4745
company_contact_person = m.ForeignKey(CompanyContactPerson)
4846

47+
def clean(self):
48+
"""
49+
Make sure that the company for client is the same as the company for
50+
the customer contact person.
51+
"""
52+
if not self.client.company == self.company_contact_person.company:
53+
raise ValidationError('The clients and the contacts company does'
54+
' not match.')
55+
4956
def __unicode__(self):
5057
return u'Case: %d' % self.id
5158
```
@@ -117,7 +124,7 @@ class CaseAdmin(admin.ModelAdmin):
117124
"""
118125
if db_field.name == "company_contact_person":
119126
kwargs['widget'] = CompanyContactPersonWidget(
120-
db_field=db_field,
127+
base_field=db_field,
121128
modeladmin=self,
122129
request=request,
123130
)

flexselect/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ class Media:
9393
js.append('%s/jqueryui/1.8.13/jquery-ui.min.js' % googlecdn)
9494
js.append('flexselect/js/flexselect.js')
9595

96-
def __init__(self, db_field, modeladmin, request, *args,
96+
def __init__(self, base_field, modeladmin, request, *args,
9797
**kwargs):
9898

99-
self.base_field = db_field
99+
self.base_field = base_field
100100
self.modeladmin = modeladmin
101101
self.request = request
102102

103103
self.hashed_name = self._hashed_name()
104104
FlexSelectWidget.instances[self.hashed_name] = self
105105
super(FlexSelectWidget, self).__init__(*args, **kwargs)
106-
106+
107107
def _hashed_name(self):
108108
"""
109109
Each widget will be unique by the name of the field and the class name
@@ -179,4 +179,4 @@ def queryset(self, instance):
179179
def empty_choices_text(self, instance):
180180
raise NotImplementedError
181181

182-
182+

test_flexselect/test_flexselect

-2 KB
Binary file not shown.

test_flexselect/tests/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs):
6363
"""
6464
if db_field.name == "company_contact_person":
6565
kwargs['widget'] = CompanyContactPersonWidget(
66-
db_field=db_field,
66+
base_field=db_field,
6767
modeladmin=self,
6868
request=request,
6969
)
@@ -83,4 +83,4 @@ class CompanyAdmin(admin.ModelAdmin):
8383
admin.site.register(Case, CaseAdmin)
8484
admin.site.register(Client, ClientAdmin)
8585
admin.site.register(CompanyContactPerson, CompanyContactPersonAdmin)
86-
admin.site.register(Company, CompanyAdmin)
86+
admin.site.register(Company, CompanyAdmin)

test_flexselect/tests/models.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.db import models as m
2+
from django.core.exceptions import ValidationError
23

34
"""
45
No changes to the models are needed to use flexselect.
@@ -29,5 +30,14 @@ class Case(m.Model):
2930
client = m.ForeignKey(Client)
3031
company_contact_person = m.ForeignKey(CompanyContactPerson)
3132

33+
def clean(self):
34+
"""
35+
Make sure that the company for client is the same as the company for
36+
the customer contact person.
37+
"""
38+
if not self.client.company == self.company_contact_person.company:
39+
raise ValidationError('The clients and the contacts company does'
40+
' not match.')
41+
3242
def __unicode__(self):
33-
return u'Case: %d' % self.id
43+
return u'Case: %d' % self.id

0 commit comments

Comments
 (0)