1 | from django.db import models
|
---|
2 |
|
---|
3 | class Category(models.Model):
|
---|
4 | name = models.CharField(maxlength=20)
|
---|
5 | url = models.CharField('The URL', maxlength=40)
|
---|
6 |
|
---|
7 | def __str__(self):
|
---|
8 | return self.name
|
---|
9 |
|
---|
10 | class Writer(models.Model):
|
---|
11 | name = models.CharField(maxlength=50, help_text='Use both first and last names.')
|
---|
12 |
|
---|
13 | def __str__(self):
|
---|
14 | return self.name
|
---|
15 |
|
---|
16 | class Article(models.Model):
|
---|
17 | headline = models.CharField(maxlength=50)
|
---|
18 | pub_date = models.DateField()
|
---|
19 | created = models.DateField(editable=False)
|
---|
20 | writer = models.ForeignKey(Writer)
|
---|
21 | article = models.TextField()
|
---|
22 | categories = models.ManyToManyField(Category, blank=True)
|
---|
23 |
|
---|
24 | def save(self):
|
---|
25 | import datetime
|
---|
26 | if not self.id:
|
---|
27 | self.created = datetime.date.today()
|
---|
28 | return super(Article, self).save()
|
---|
29 |
|
---|
30 | def __str__(self):
|
---|
31 | return self.headline
|
---|
32 |
|
---|
33 | class PhoneNumber(models.Model):
|
---|
34 | phone = models.PhoneNumberField()
|
---|
35 | description = models.CharField(maxlength=20)
|
---|
36 |
|
---|
37 | def __str__(self):
|
---|
38 | return self.phone
|
---|
39 |
|
---|
40 | def CategoryCallback(f, **kwargs):
|
---|
41 | if not "cat_" in f.name:
|
---|
42 | f.name = 'cat_'+f.name
|
---|
43 | return f.formfield(**kwargs)
|
---|
44 |
|
---|
45 |
|
---|
46 | __test__ = {'API_TESTS': """
|
---|
47 | >>> from django.newforms import form_for_model, form_for_instance, save_instance, BaseForm, Form, CharField
|
---|
48 | >>> import datetime
|
---|
49 |
|
---|
50 | >>> Category.objects.all()
|
---|
51 | []
|
---|
52 |
|
---|
53 | >>> CategoryForm = form_for_model(Category, formfield_callback=CategoryCallback)
|
---|
54 | >>> f = CategoryForm()
|
---|
55 | >>> print f
|
---|
56 | <tr><th><label for="id_cat_name">Name:</label></th><td><input id="id_cat_name" type="text" name="cat_name" maxlength="20" /></td></tr>
|
---|
57 | <tr><th><label for="id_cat_url">The URL:</label></th><td><input id="id_cat_url" type="text" name="cat_url" maxlength="40" /></td></tr>
|
---|
58 | >>> print f.as_ul()
|
---|
59 | <li><label for="id_cat_name">Name:</label> <input id="id_cat_name" type="text" name="cat_name" maxlength="20" /></li>
|
---|
60 | <li><label for="id_cat_url">The URL:</label> <input id="id_cat_url" type="text" name="cat_url" maxlength="40" /></li>
|
---|
61 | >>> print f['name']
|
---|
62 | Traceback (most recent call last):
|
---|
63 | ...
|
---|
64 | KeyError: "Key 'name' not found in Form"
|
---|
65 | >>> print f['cat_name']
|
---|
66 | <input id="id_cat_name" type="text" name="cat_name" maxlength="20" />
|
---|
67 |
|
---|
68 | # this works, if there is no ForeignKeys
|
---|
69 | >>> Category(name="foo", url="http://www.example.com")
|
---|
70 | <Category: foo>
|
---|
71 |
|
---|
72 | # this works
|
---|
73 | >>> w = Writer(name='Bob Woodward')
|
---|
74 | >>> Article(headline='Test article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.')
|
---|
75 | <Article: Test article>
|
---|
76 |
|
---|
77 | # now lets try one with ForeignKey
|
---|
78 | >>> ArticleForm = form_for_model(Article, formfield_callback=CategoryCallback)
|
---|
79 | >>> f = ArticleForm(auto_id=False)
|
---|
80 | >>> print f
|
---|
81 | <tr><th>Headline:</th><td><input type="text" name="cat_headline" maxlength="50" /></td></tr>
|
---|
82 | <tr><th>Pub date:</th><td><input type="text" name="cat_pub_date" /></td></tr>
|
---|
83 | <tr><th>Writer:</th><td><select name="cat_writer">
|
---|
84 | <option value="" selected="selected">---------</option>
|
---|
85 | </select></td></tr>
|
---|
86 | <tr><th>Article:</th><td><textarea name="cat_article"></textarea></td></tr>
|
---|
87 | <tr><th>Categories:</th><td><select multiple="multiple" name="cat_categories">
|
---|
88 | </select><br /> Hold down "Control", or "Command" on a Mac, to select more than one.</td></tr>
|
---|
89 | >>> w = Writer(name='Bob Woodward')
|
---|
90 |
|
---|
91 | #but now the same one fails, even though Article object as itself should not be modified by for_for_model
|
---|
92 | >>> Article(headline='Test article', pub_date=datetime.date(1988, 1, 4), writer=w, article='Hello.')
|
---|
93 | """}
|
---|