2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2007-03-24 04:17:04 +08:00
|
|
|
27. Default manipulators
|
2006-06-01 02:45:17 +08:00
|
|
|
|
|
|
|
Each model gets an AddManipulator and ChangeManipulator by default.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Musician(models.Model):
|
|
|
|
first_name = models.CharField(maxlength=30)
|
|
|
|
last_name = models.CharField(maxlength=30)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s %s" % (self.first_name, self.last_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class Album(models.Model):
|
|
|
|
name = models.CharField(maxlength=100)
|
|
|
|
musician = models.ForeignKey(Musician)
|
|
|
|
release_date = models.DateField(blank=True, null=True)
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.name
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> from django.utils.datastructures import MultiValueDict
|
|
|
|
|
|
|
|
# Create a Musician object via the default AddManipulator.
|
|
|
|
>>> man = Musician.AddManipulator()
|
|
|
|
>>> data = MultiValueDict({'first_name': ['Ella'], 'last_name': ['Fitzgerald']})
|
|
|
|
|
|
|
|
>>> man.get_validation_errors(data)
|
|
|
|
{}
|
|
|
|
>>> man.do_html2python(data)
|
|
|
|
>>> m1 = man.save(data)
|
|
|
|
|
|
|
|
# Verify it worked.
|
|
|
|
>>> Musician.objects.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Musician: Ella Fitzgerald>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> [m1] == list(Musician.objects.all())
|
|
|
|
True
|
|
|
|
|
|
|
|
# Attempt to add a Musician without a first_name.
|
|
|
|
>>> man.get_validation_errors(MultiValueDict({'last_name': ['Blakey']}))
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
{'first_name': [u'This field is required.']}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Attempt to add a Musician without a first_name and last_name.
|
|
|
|
>>> man.get_validation_errors(MultiValueDict({}))
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
{'first_name': [u'This field is required.'], 'last_name': [u'This field is required.']}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Attempt to create an Album without a name or musician.
|
|
|
|
>>> man = Album.AddManipulator()
|
|
|
|
>>> man.get_validation_errors(MultiValueDict({}))
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
{'musician': [u'This field is required.'], 'name': [u'This field is required.']}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Attempt to create an Album with an invalid musician.
|
|
|
|
>>> man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['foo']}))
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
{'musician': [u"Select a valid choice; 'foo' is not in ['', '1']."]}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Attempt to create an Album with an invalid release_date.
|
|
|
|
>>> man.get_validation_errors(MultiValueDict({'name': ['Sallies Fforth'], 'musician': ['1'], 'release_date': 'today'}))
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
{'release_date': [u'Enter a valid date in YYYY-MM-DD format.']}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Create an Album without a release_date (because it's optional).
|
|
|
|
>>> data = MultiValueDict({'name': ['Ella and Basie'], 'musician': ['1']})
|
|
|
|
>>> man.get_validation_errors(data)
|
|
|
|
{}
|
|
|
|
>>> man.do_html2python(data)
|
|
|
|
>>> a1 = man.save(data)
|
|
|
|
|
|
|
|
# Verify it worked.
|
|
|
|
>>> Album.objects.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Album: Ella and Basie>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Album.objects.get().musician
|
2006-06-04 08:23:51 +08:00
|
|
|
<Musician: Ella Fitzgerald>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Create an Album with a release_date.
|
|
|
|
>>> data = MultiValueDict({'name': ['Ultimate Ella'], 'musician': ['1'], 'release_date': ['2005-02-13']})
|
|
|
|
>>> man.get_validation_errors(data)
|
|
|
|
{}
|
|
|
|
>>> man.do_html2python(data)
|
|
|
|
>>> a2 = man.save(data)
|
|
|
|
|
|
|
|
# Verify it worked.
|
|
|
|
>>> Album.objects.order_by('name')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Album: Ella and Basie>, <Album: Ultimate Ella>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> a2 = Album.objects.get(pk=2)
|
|
|
|
>>> a2
|
2006-06-04 08:23:51 +08:00
|
|
|
<Album: Ultimate Ella>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> a2.release_date
|
|
|
|
datetime.date(2005, 2, 13)
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|