To run this code, written at the very end of Chapter 3, you need a working empty database.
To move the project into a valid state, please use the command git chapter [chapter-number] to find a valid commit. A commit at the end of Chapter 3, or any commit in Chapters 4-9, should work just fine. Use git checkout [commit] to change the state of the project. Once there, delete the database (if it exists).
$ rm db.sqlite3
To (re-)create the database:
$ ./manage.py migrate
Please see the Read Me file or the actual book for more details.
In [1]:
from datetime import date
from organizer.models import Tag, Startup, NewsLink
from blog.models import Post
In [2]:
edut = Tag(name='Education', slug='education')
In [3]:
edut
Out[3]:
In [4]:
edut.save()
In [5]:
edut.delete()
In [6]:
edut # still in memory!
Out[6]:
In [7]:
type(Tag.objects) # a model manager
Out[7]:
In [8]:
Tag.objects.create(name='Video Games', slug='video-games')
Out[8]:
In [9]:
# create multiple objects in a go!
Tag.objects.bulk_create([
Tag(name='Django', slug='django'),
Tag(name='Mobile', slug='mobile'),
Tag(name='Web', slug='web'),
])
Out[9]:
In [10]:
Tag.objects.all()
Out[10]:
In [11]:
Tag.objects.all()[0] # acts like a list
Out[11]:
In [12]:
type(Tag.objects.all()) # is not a list
Out[12]:
In [13]:
# managers are not accessible to model instances, only to model classes!
try:
edut.objects
except AttributeError as e:
print(e)
In [14]:
Tag.objects.all()
Out[14]:
In [15]:
Tag.objects.count()
Out[15]:
In [16]:
Tag.objects.get(slug='django')
Out[16]:
In [17]:
type(Tag.objects.all())
Out[17]:
In [18]:
type(Tag.objects.get(slug='django'))
Out[18]:
In [19]:
# case-sensitive!
try:
Tag.objects.get(slug='Django')
except Tag.DoesNotExist as e:
print(e)
In [20]:
# the i is for case-Insensitive
Tag.objects.get(slug__iexact='DJANGO')
Out[20]:
In [21]:
Tag.objects.get(slug__istartswith='DJ')
Out[21]:
In [22]:
Tag.objects.get(slug__contains='an')
Out[22]:
In [23]:
# get always returns a single object
try:
# djangO, mObile, videO-games
Tag.objects.get(slug__contains='o')
except Tag.MultipleObjectsReturned as e:
print(e)
In [24]:
## unlike get, can fetch multiple objects
Tag.objects.filter(slug__contains='o')
Out[24]:
In [25]:
type(Tag.objects.filter(slug__contains='o'))
Out[25]:
In [26]:
Tag.objects.filter(slug__contains='o').order_by('-name')
Out[26]:
In [27]:
# first we call order_by on the manager
Tag.objects.order_by('-name')
Out[27]:
In [28]:
# now we call filter on the manager, and order the resulting queryset
Tag.objects.filter(slug__contains='e').order_by('-name')
Out[28]:
In [29]:
Tag.objects.values_list()
Out[29]:
In [30]:
type(Tag.objects.values_list())
Out[30]:
In [31]:
Tag.objects.values_list('name', 'slug')
Out[31]:
In [32]:
Tag.objects.values_list('name')
Out[32]:
In [33]:
Tag.objects.values_list('name', flat=True)
Out[33]:
In [34]:
type(Tag.objects.values_list('name', flat=True))
Out[34]:
In [35]:
jb = Startup.objects.create(
name='JamBon Software',
slug='jambon-software',
contact='django@jambonsw.com',
description='Web and Mobile Consulting.\n'
'Django Tutoring.\n',
founded_date=date(2013, 1, 18),
website='https://jambonsw.com/',
)
jb # this output only clear because of __str__()
Out[35]:
In [36]:
jb.founded_date
Out[36]:
In [37]:
jb.founded_date = date(2014,1,1)
# we're not calling save() !
jb.founded_date
Out[37]:
In [38]:
# get version in database
jb = Startup.objects.get(slug='jambon-software')
# work above is all for nought because we didn't save()
jb.founded_date
Out[38]:
In [39]:
djt = Post.objects.create(
title='Django Training',
slug='django-training',
text=(
"Learn Django in a classroom setting "
"with JamBon Software."),
)
djt
Out[39]:
In [40]:
djt.pub_date = date(2013, 1, 18)
djt.save()
djt
Out[40]:
In [41]:
type(djt.tags)
Out[41]:
In [42]:
type(djt.startups)
Out[42]:
In [43]:
djt.tags.all()
Out[43]:
In [44]:
djt.startups.all()
Out[44]:
In [45]:
django = Tag.objects.get(slug__contains='django')
djt.tags.add(django)
djt.tags.all()
Out[45]:
In [46]:
django.blog_posts.all() # a "reverse" relation
Out[46]:
In [47]:
django.startup_set.add(jb) # a "reverse" relation
django.startup_set.all()
Out[47]:
In [48]:
jb.tags.all() # the "forward" relation
Out[48]:
In [49]:
# on more time, for repetition!
djt
Out[49]:
In [50]:
# "forward" relation
djt.startups.add(jb)
djt.startups.all()
Out[50]:
In [51]:
jb.blog_posts.all() # "reverse" relation
Out[51]: