~grupoesoc/cubicerp-addons/7.0

« back to all changes in this revision

Viewing changes to report_geraldo/lib/geraldo/site/newsite/django_1_0/tests/modeltests/get_or_create/models.py

  • Committer: Cubic ERP
  • Date: 2014-01-07 15:38:09 UTC
  • Revision ID: info@cubicerp.com-20140107153809-4jmif3zoi8rcveve
[ADD] cubicReport

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
33. get_or_create()
 
3
 
 
4
get_or_create() does what it says: it tries to look up an object with the given
 
5
parameters. If an object isn't found, it creates one with the given parameters.
 
6
"""
 
7
 
 
8
from django.db import models
 
9
 
 
10
class Person(models.Model):
 
11
    first_name = models.CharField(max_length=100)
 
12
    last_name = models.CharField(max_length=100)
 
13
    birthday = models.DateField()
 
14
 
 
15
    def __unicode__(self):
 
16
        return u'%s %s' % (self.first_name, self.last_name)
 
17
 
 
18
__test__ = {'API_TESTS':"""
 
19
# Acting as a divine being, create an Person.
 
20
>>> from datetime import date
 
21
>>> p = Person(first_name='John', last_name='Lennon', birthday=date(1940, 10, 9))
 
22
>>> p.save()
 
23
 
 
24
# Only one Person is in the database at this point.
 
25
>>> Person.objects.count()
 
26
1
 
27
 
 
28
# get_or_create() a person with similar first names.
 
29
>>> p, created = Person.objects.get_or_create(first_name='John', last_name='Lennon', defaults={'birthday': date(1940, 10, 9)})
 
30
 
 
31
# get_or_create() didn't have to create an object.
 
32
>>> created
 
33
False
 
34
 
 
35
# There's still only one Person in the database.
 
36
>>> Person.objects.count()
 
37
1
 
38
 
 
39
# get_or_create() a Person with a different name.
 
40
>>> p, created = Person.objects.get_or_create(first_name='George', last_name='Harrison', defaults={'birthday': date(1943, 2, 25)})
 
41
>>> created
 
42
True
 
43
>>> Person.objects.count()
 
44
2
 
45
 
 
46
# If we execute the exact same statement, it won't create a Person.
 
47
>>> p, created = Person.objects.get_or_create(first_name='George', last_name='Harrison', defaults={'birthday': date(1943, 2, 25)})
 
48
>>> created
 
49
False
 
50
>>> Person.objects.count()
 
51
2
 
52
"""}