1 | from django.db import models
|
---|
2 | from django import forms
|
---|
3 |
|
---|
4 | class Department(models.Model):
|
---|
5 | name = models.CharField(max_length=200, blank=False)
|
---|
6 |
|
---|
7 | class PeopleSearchForm(forms.Form):
|
---|
8 | people_s = forms.CharField(label='Search', required=False)
|
---|
9 | people_d = forms.ModelChoiceField(queryset=Department.objects.all(), empty_label='-- department --', required=False)
|
---|
10 | people_z = forms.ModelMultipleChoiceField(queryset=Department.objects.all(), required=False)
|
---|
11 |
|
---|
12 |
|
---|
13 | __test__ = {'API_TESTS':"""
|
---|
14 |
|
---|
15 | >>> Department.objects.create(name='Monty')
|
---|
16 | <Department: Department object>
|
---|
17 | >>> Department.objects.create(name='Brian')
|
---|
18 | <Department: Department object>
|
---|
19 |
|
---|
20 | # Pass data to the PeopleSearchForm #
|
---|
21 | # Empty Form
|
---|
22 | >>> f = PeopleSearchForm({})
|
---|
23 | >>> f.is_valid()
|
---|
24 | True
|
---|
25 |
|
---|
26 | # Valid search
|
---|
27 | >>> f = PeopleSearchForm({'people_s': 'Monty'})
|
---|
28 | >>> f.is_valid()
|
---|
29 | True
|
---|
30 |
|
---|
31 | # Invalid department search
|
---|
32 | >>> f = PeopleSearchForm({'people_d': 'Monty'})
|
---|
33 | >>> f.is_valid()
|
---|
34 | False
|
---|
35 |
|
---|
36 | # Valid department multiple select
|
---|
37 | >>> f = PeopleSearchForm({'people_z': [1,2]})
|
---|
38 | >>> f.is_valid()
|
---|
39 | True
|
---|
40 |
|
---|
41 | # invalid department multiple select
|
---|
42 | >>> f = PeopleSearchForm({'people_z': ['monty']})
|
---|
43 | >>> f.is_valid()
|
---|
44 | False
|
---|
45 | """}
|
---|