django shell
Date: March 18th 2016
Last updated: March 18th 2016
After creating a database model it is a good idea to open up the django shell and test the relationships between records. This entry continues from the previous example (create model). However, several edits have been made to models.py which I have added to the end of this entry.
The goal of this is to remind myself how to access, query and update records using the django shell.
Update database schema
python3 manage.py flush
python3 manage.py makemigrations surferprofile
python3 manage.py migrate
open django shell
python3 manage.py shell
import app
from surferprofile.models import *
add beaches
b1 = Beach(beachname="Lyall Bay", city="Wellington", suburb="Lyall Bay", country="New Zealand")
b2 = Beach(beachname="Maroubra", city="Sydney", suburb="Maroubra", country="Australia")
b3 = Beach(beachname="Seal Rocks", country="Australia")
# ALWAYS SAVE
b1.save(), b2.save(), b3.save()
Query beaches (results not shown)
# search all beaches
Beach.objects.all()
# get first entry
Beach.objects.get(pk=1)
# filter
Beach.objects.filter(beachname="Lyall Bay")
# get name starting with char*
Beach.objects.filter(beachname__startswith = "Lyall")
add waves (Foreign key example; Wave ---> Beach)
# default for break (beach break)
# default for wavetype (tubing)
# beach is a FOREIGN KEY (refer to Beach; i.e. b1, b2 and b3)
w1 = Wave(wavename = 'Main Beach', wavetype='CO', beach = b1)
w2 = Wave(wavename = 'The Wall', beach = b1)
w3 = Wave(wavename = 'Dunnybowls', wavetype='RE', beach = b2)
w4 = Wave(wavename = 'Main Beach', beach = b2)
w5 = Wave(wavename = "Seal Rocks", wavebreak="PB", wavetype="CR", beach = b3)
# SAVE!
w1.save(), w2.save(), w3.save(), w4.save(), w5.save()
Add boards, fins and tricks
# ADD BOARDS
board1 = Board(boardlength=6.4, boardwidth=18.5, boardthickness=2.38, boardmake="hectic")
# ADD FINS
fins1 = Fin(finsystem="FCS", fintemplate="TRI", name="K3")
fins2 = Fin(finsystem="FCS", fintemplate="TRI", name="PC3")
# ADD FAV TRICKS
trick1 = TRICK(trickname = "Larry Layback")
trick2 = TRICK(trickname = "Roundhouse cutback")
trick3 = TRICK(trickname = "Float the boat")
trick4 = TRICK(trickname = "Reo")
trick5 = TRICK(trickname = "Head dip shampoo")
# SAVE
board1.save(), fins1.save(), fins2.save()
trick1.save(), trick2.save(), trick3.save(), trick4.save(), trick5.save()
Add surfer (ManyToMany example; e.g. Surfer <---> Wave)
# ADD SURFER
s1 = Surfer(firstname="Ray", lastname="Blick",
height=186.0, weight=82.0,
hometown= "Wainuiomata, NZ")
# SAVE
s1.save()
# add MANYTOMANY relationships
s1.homebeach.add(b1)
s1.favwave.add(w4, w5)
s1.boards.add(board1)
s1.fins.add(fins1, fins2)
s1.tricks.add(trick2, trick5)
Updated models.py
from django.db import models
class Beach(models.Model):
beachname = models.CharField(max_length=20)
city = models.CharField(max_length=20, null=True)
suburb = models.CharField(max_length=20, null=True)
country = models.CharField(max_length=20, null=True)
postcode = models.IntegerField(null=True)
def __str__(self):
return '{}'.format(self.beachname)
class Wave(models.Model):
# break choice
POINTBREAK = 'PB'
BEACHBREAK = 'BB'
REEF = 'RF'
WAVECHOICE = ((POINTBREAK, 'Point break'), \
(BEACHBREAK, 'Beach break'), \
(REEF, 'Reef'),)
wavebreak = models.CharField(max_length=2, choices=WAVECHOICE,
default=BEACHBREAK)
# Wave type
CLOSEOUTS = 'CO'
CRUMBLY = 'CR'
REFORMS = 'RE'
TUBING = 'TU'
DOUBLEUPS = 'DU'
WAVETYPE = ((CLOSEOUTS, 'Closeouts'),
(CRUMBLY, 'Crumbly'),
(REFORMS, 'Reforming'),
(TUBING, 'Hollow tubing'),
(DOUBLEUPS, 'Double-ups'),)
wavetype = models.CharField(max_length=2, choices=WAVETYPE,
default=TUBING)
wavename = models.CharField(max_length=40, null=True)
beach = models.ForeignKey(Beach)
def __str__(self):
return '{}'.format(self.wavename)
class Board(models.Model):
boardlength = models.DecimalField(max_digits=4,decimal_places=2)
boardwidth = models.DecimalField(max_digits=4,decimal_places=2, null=True)
boardthickness = models.DecimalField(max_digits=4,decimal_places=2, null=True)
boardvolume = models.DecimalField(max_digits=4,decimal_places=2, null=True)
boardmake = models.CharField(max_length=20)
boardshaper = models.CharField(max_length=40, null=True)
def __str__(self):
return '{} {}'.format(self.boardlength, self.boardmake)
class Fin(models.Model):
finsystem = models.CharField(max_length=20, null=True)
fintemplate = models.CharField(max_length=20, null=True)
name = models.CharField(max_length=20)
def __str__(self):
return '{} {}'.format(self.finsystem, self.name)
class Trick(models.Model):
trickname = models.CharField(max_length=20)
def __str__(self):
return '{}'.format(self.trickname)
class Surfer(models.Model):
firstname = models.CharField(max_length=20)
lastname = models.CharField(max_length=20, null=True)
height = models.DecimalField(max_digits=4, decimal_places=1, null=True)
weight = models.DecimalField(max_digits=4, decimal_places=1, null=True)
DOB = models.DateTimeField('DOB', null=True)
hometown = models.CharField(max_length=50, null=True)
homebeach = models.ManyToManyField(Beach)
favwave = models.ManyToManyField(Wave)
fins = models.ManyToManyField(Fin)
tricks = models.ManyToManyField(Trick)
boards = models.ManyToManyField(Board)
def __str__(self):
return '{} {}'.format(self.firstname, self.lastname)
class SurfDiary(models.Model):
wavesize = models.IntegerField(null=False)
starttime = models.DateTimeField('start time', null=True)
endtime = models.DateTimeField('end time', null=True)
SCORE_CHOICES = zip( range(1,11), range(1,11) )
wavequality = models.IntegerField(choices=SCORE_CHOICES, blank=True)
selfscore = models.IntegerField(choices=SCORE_CHOICES, blank=True)
crowdscore = models.IntegerField(choices=SCORE_CHOICES, blank=True)
NONE = 'NO'
ONSHORE = 'ON'
OFFSHORE = 'OF'
CROSSSHORE = 'CR'
WINDDIR = ((NONE, 'No wind'),
(ONSHORE, 'On shore'),
(OFFSHORE, 'Off shore'),
(CROSSSHORE, 'Cross shore'),)
winddir = models.CharField(max_length=2, choices=WINDDIR, null=True)
notes = models.CharField(max_length=200, null=True)
surfer = models.ForeignKey(Surfer)
board = models.ForeignKey(Board)
fin = models.ForeignKey(Fin)
beach = models.ForeignKey(Beach)
wave = models.ForeignKey(Wave)
trick = models.ForeignKey(Trick)
def __str__(self):
return '{} surfed {} ft waves at {}'.format(self.surfer,
self.wavesize,
self.beach)