📺 Develpreneur YouTube Episode

Video + transcript

Learn Python And Django Day 40

2021-03-16 •Youtube

Detailed Notes

This is a 1st part of our look at more complex queries and sending parameters into a raw query in Django.

You can view source for today and all prior days by reviewing the tag Day 41 at this GitHub (this one includes day 41 as well): https://github.com/robbroadhead/develpreneur-pythontutorial

Transcript Text
[Music]
hello and welcome back
we are continuing our uh
cleaning up and user experience side of
this
application so we are uh last time
around
we added this has four children so
in the um roadmaps we're able to see
from the top list sort of what
what's been filled out and what hasn't
and now let's take that
down a level and we're gonna actually
look at the uh
time frames because at this point you
have and time frames a little different
from roadmap roadmap just has
time frames underneath it time frame
actually has two things
it has if you look it has child time
frames potentially
and goals for the period uh one other
thing i'm gonna do is i'm gonna tweak
this um here so that we're not our
footer is not hanging over
our uh lists but i probably won't get to
that this time
uh but we will start with uh we're gonna
add two things here we're going to have
child time frames and then we're going
to have tasks because a given time frame
could have tasks
directly attac or we'll say goals
because they could be directly attied to
it as we have here so we're gonna go
back over this is gonna be more
from our queries we're gonna do another
raw query so when we
list roadmaps we are now going to have
uh within this when we do list well
there's two things
let's start here so this is on our time
frame so let's go to
our time frame we'll take a look at that
real quick so
time frame is that a specific one or is
that
okay so we have two we have a listing of
time frames so we have them underneath
we'll stick with the one underneath
which is the one with an id
uh that's actually edit i'm sorry
oh yeah that's what we want whoops
that's what we want so if we go to our
goals
and we hit a time frame so this is
looking at a specific time frame
okay so it's tf1 which is edit time
frame
and so let's go look at that real quick
and we look in here we come in
and data is just this time frame objects
we're just getting a list
of time frames within that
so what we need to do is this instead is
going to be
instead of objects get it's going to be
objects.raw
and we're still going to have it based
on there's going to be an id so we'll
take a look at this
because we're going to have end up
having a parameter
and if we look at our prior run
here it was just a straight query now
we're gonna actually work
on adding a parameter into that as well
so
let's make sure i got that right
object's raw yeah
objects raw okay
so let's figure out our query so start
with this
we're this time we're going to start
with our time frames
so we can do it most simply is
this is the bulk of what we've got uh
except for we're going to have a
an id whoops
that's something i want to flip over to
so because here
i want child time frame so i want
everybody
in this case i'm sending a one across
so it's technically going to be it's
going to be a dp time frame where
parent id
equals one so that's our parameter so
here's
and if we look i have first quarter
second quarter goals
first quarter second quarter goals but
now we have to expand that out because
we need to go
look at what the children of that is
and the way we do that it goes back to
we're going to create a left join
back to the same table but we're going
to join where parent id
equals myself essentially uh
in each case this would be self
and let's see so that's our left join
and then i'm just going to do where
like we had it before where parent id
equals 1 except for now i have to go off
this primary this is the main table
i'm searching off of this
tf2 here is the second table i'm going
to be is the one that's going to be
essentially the children
so i want to make sure where the
so where that and so this is where uh
tf1 dot parent id
equals that so now what i'm going to see
is that neither of these have children
which i think we saw that here so if i
look at first quarter it doesn't have
any child time frames
if you go to second quarter it doesn't
have any child time frames
but let's do first quarter i'm going to
add some time frames
i'm going to add uh january
and save and this is something we want
to do as well we're going to want to go
back
and have the ability to go back to this
guy's parent
which we did not add here so i've got
january and i'm going to create february
and i'm going to create march
so this will give us something to be
able to look at
in our query so now if we look at our
query we're going to see
uh same query we're going to see now
that we get january february march
for first quarter and then nothing for
second quarter
now we also want to do uh the
tasks or the goals so that means we're
also going to left join back
into uh itp project
underscore and what did we call that we
called
it oops over here models
we call it goals called it task
whoops so if we go dp project task
and we're going to call it t
on t dot what did we call it whoops
and the parent to that is actually time
frame
so we go over here so we're t dot time
frame
equals and then this is going to be off
of the parent one not the child one
so equals tf1 dot id
and t.time frame
so i wonder if i not get that right
let's see
so if i look at my model
task time frame well let's take a look
at tasks did i not maybe i missed
something here let's see
if i do a make migrations
let's make sure i don't nope no changes
i don't think i have to migrate
no migrations to apply oh
let's do can i do all
oh it says none
so let's just make sure one more time so
i do dp project
yeah there's no migration surprise okay
so let's go take a look back at my query
uh let's see so it says
unknown column t
at time frame
so if we describe dp project
underscore task
oh it's time frame id my mistake so we
need to do the time frame id
and so now we see it's starting to get a
little bit long but we have
here is a row where we're going to see
that this has got january
oh and there's no tasks tied to
uh each of those do i have no tasks to
that let's create some tasks on that guy
so if i go back to oh i should have it
oops let's run the server
okay so running the server
if i look at here i should have goals i
should have
multiple goals uh and this is the time
frame so
if i do
oh and let me actually pull this out
since i'm sort of working on this on the
fly
this is going to be here let's just do
it like this for now
uh so we want to left join task on its
time frame id
equals the primary id
oh uh oh no i'm sorry this is not
actually off of the time frame off the
parent this is actually
off of he's actually going to be the
time frame id equals
in this case 1 as well
because i'm actually i want the tasks
for this
this guy not these inner guys
so now if i do that i'm going to see
let's see so let's do this one so now i
see a much longer one
and i can see i'm going to start i'm
seeing duplicates all over the place
because i'm doing all these left joins
so this goes back to what we
saw last time where we're going to do
um
there we go where one of the things we
did is we started to do
group by
so where is that at
so now what we want to do is take in our
long query wherever that went
let's just do this i think i missed it
there it is
okay so for here now there's two things
we're gonna group by this can become a
little bit
annoying so we're gonna go group by
first
let's group by um
how do we want to do this one oh well
first let's do tf.star
tf1.star so we're going to take
everything from here so let's do that
first
and that's going to clean it up a little
because we're going to start seeing
where our duplicates exist
so see we can see a bunch of duplicates
here
so now that means
darn it let's come back over here to our
view let's so the first thing we're
going to group by is we've got two
things we need to group by we need to
group by
the uh
tf dot parent id so we're going to group
by actually
we're in group by tf2 dot
id
and so if we group by the tf2 dot id
uh here
did i yeah i get my group yeah i got it
so now
we're gonna see only second quarter
goals plus the three first quarter
because remember
we have what we're going to see is we
have three goals for a period
so second quarter goal shows up once but
then we're going to see
first quarter because we have three
tasks so now we have to
uh and with that that means we want to
uh whoops
let's go reviews and then here the first
things we're going to do is we're going
to do
count um
so we have two counts one is going to be
uh
children and the
other is going to be tasks
so we need to group by tf2 id and we
also need to group
by the task dot id
so we group by task id we're going to
see it's not really going to change
but we are going to count so children we
need to count the tf2.id
and i think we can do this and if we
count the
t dot id we're gonna get tasks let's see
what we get here
if we go back over here
so we're not getting that one right so
we missed something so let's go back to
this so we count that we're going to
group by that
may have to group this twice because it
may be a little bit different
oh that is it so we have to gr we have
to do this twice now so this one becomes
a little bit more complicated
so we actually have to do
well let's do the children first
and then we're going to come back
because this is going to actually be
a little more complicated
which may take us into so we're probably
not that's going to be a two-parter
because okay so the first thing we want
to do is we want to get our children and
then that's going to be
based off of tf2
so if we do that we go over here
well
oh did i lose i lost my where somewhere
in there
uh whoops views
here it is uh so where
so if i do that
i think that's going to give me what i
need
oh now i got a count
oh did i group that right let's go look
back at my v
select so i'm going to select star
group by that
so tf1 oh that needs to be count
where tf1 equals that
let's see i think that will get us there
no we're not quite there because it's
not counting us so we're not grouping
something right
so if we look here we're going to select
tf1
we're going to left join on the time
frame
on tf2 dot parent id equals that so
maybe we can count tf
i don't want to group by tf 2 on a group
by tf1
and then this is going to be tf1.star
count
and this is going to be children
tf2.id so now
we should see it there we go so now we
see our children
so now we're gonna since we're doing two
groupings this is kind of we're gonna
actually build on this
and uh we're gonna take care of that up
next episode it's gonna be a nice little
two-parter
so i'm gonna wrap this one up we've gone
a little long i forgot
how complicated this one was gonna be
that's what happens when you do stuff on
the fly
so uh we'll come back around to it but
until then
go out have yourself a great day a great
week and we will talk to you
next time
you
Transcript Segments
0.62

[Music]

26.32

hello and welcome back

27.68

we are continuing our uh

30.8

cleaning up and user experience side of

33.12

this

33.92

application so we are uh last time

36.559

around

38.16

we added this has four children so

41.36

in the um roadmaps we're able to see

44.239

from the top list sort of what

46.079

what's been filled out and what hasn't

48.079

and now let's take that

49.36

down a level and we're gonna actually

51.199

look at the uh

52.559

time frames because at this point you

54.239

have and time frames a little different

56

from roadmap roadmap just has

58.399

time frames underneath it time frame

60.48

actually has two things

61.68

it has if you look it has child time

64.32

frames potentially

65.439

and goals for the period uh one other

68.4

thing i'm gonna do is i'm gonna tweak

70.159

this um here so that we're not our

74.159

footer is not hanging over

76.4

our uh lists but i probably won't get to

79.04

that this time

81.119

uh but we will start with uh we're gonna

84

add two things here we're going to have

87.439

child time frames and then we're going

88.96

to have tasks because a given time frame

91.28

could have tasks

92.799

directly attac or we'll say goals

97.36

because they could be directly attied to

98.88

it as we have here so we're gonna go

100.64

back over this is gonna be more

102.64

from our queries we're gonna do another

105.84

raw query so when we

107.6

list roadmaps we are now going to have

113.119

uh within this when we do list well

116.159

there's two things

117.04

let's start here so this is on our time

120.32

frame so let's go to

121.36

our time frame we'll take a look at that

123.68

real quick so

124.479

time frame is that a specific one or is

127.52

that

128.16

okay so we have two we have a listing of

129.599

time frames so we have them underneath

131.12

we'll stick with the one underneath

133.36

which is the one with an id

137.52

uh that's actually edit i'm sorry

140.8

oh yeah that's what we want whoops

142.4

that's what we want so if we go to our

143.92

goals

145.04

and we hit a time frame so this is

148.16

looking at a specific time frame

149.92

okay so it's tf1 which is edit time

152.16

frame

155.04

and so let's go look at that real quick

160.48

and we look in here we come in

165.12

and data is just this time frame objects

167.76

we're just getting a list

169.12

of time frames within that

172.72

so what we need to do is this instead is

175.599

going to be

178.08

instead of objects get it's going to be

182.84

objects.raw

184

and we're still going to have it based

185.44

on there's going to be an id so we'll

187.12

take a look at this

188.239

because we're going to have end up

189.44

having a parameter

193.599

and if we look at our prior run

198.8

here it was just a straight query now

201.84

we're gonna actually work

202.8

on adding a parameter into that as well

204.879

so

207.92

let's make sure i got that right

209.599

object's raw yeah

211.2

objects raw okay

214.319

so let's figure out our query so start

217.519

with this

218

we're this time we're going to start

219.36

with our time frames

222.64

so we can do it most simply is

225.76

this is the bulk of what we've got uh

228

except for we're going to have a

230.48

an id whoops

233.84

that's something i want to flip over to

235.76

so because here

237.12

i want child time frame so i want

238.84

everybody

240.879

in this case i'm sending a one across

244.799

so it's technically going to be it's

246.4

going to be a dp time frame where

251.04

parent id

255.04

equals one so that's our parameter so

258

here's

258.72

and if we look i have first quarter

261.759

second quarter goals

263.28

first quarter second quarter goals but

265.199

now we have to expand that out because

266.8

we need to go

268.479

look at what the children of that is

272

and the way we do that it goes back to

273.68

we're going to create a left join

275.36

back to the same table but we're going

277.52

to join where parent id

278.639

equals myself essentially uh

282

in each case this would be self

286.4

and let's see so that's our left join

289.04

and then i'm just going to do where

290.4

like we had it before where parent id

293.68

equals 1 except for now i have to go off

296.32

this primary this is the main table

298.32

i'm searching off of this

301.52

tf2 here is the second table i'm going

304.08

to be is the one that's going to be

305.28

essentially the children

306.88

so i want to make sure where the

311.36

so where that and so this is where uh

314.52

tf1 dot parent id

317.52

equals that so now what i'm going to see

319.68

is that neither of these have children

322.24

which i think we saw that here so if i

323.919

look at first quarter it doesn't have

325.199

any child time frames

327.199

if you go to second quarter it doesn't

329.12

have any child time frames

331.84

but let's do first quarter i'm going to

333.199

add some time frames

335.36

i'm going to add uh january

340.72

and save and this is something we want

344.639

to do as well we're going to want to go

345.84

back

346.56

and have the ability to go back to this

350.479

guy's parent

353.12

which we did not add here so i've got

356.16

january and i'm going to create february

364.4

and i'm going to create march

368.639

so this will give us something to be

370.16

able to look at

372.24

in our query so now if we look at our

373.919

query we're going to see

375.44

uh same query we're going to see now

378

that we get january february march

380.16

for first quarter and then nothing for

381.919

second quarter

383.52

now we also want to do uh the

386.72

tasks or the goals so that means we're

389.759

also going to left join back

391.84

into uh itp project

398

underscore and what did we call that we

401.12

called

402

it oops over here models

406.24

we call it goals called it task

410.319

whoops so if we go dp project task

415.039

and we're going to call it t

418.639

on t dot what did we call it whoops

423.12

and the parent to that is actually time

425.68

frame

428.96

so we go over here so we're t dot time

431.759

frame

433.36

equals and then this is going to be off

435.12

of the parent one not the child one

437.12

so equals tf1 dot id

443.599

and t.time frame

448.72

so i wonder if i not get that right

450.4

let's see

454.319

so if i look at my model

459.84

task time frame well let's take a look

462.72

at tasks did i not maybe i missed

464.4

something here let's see

466.319

if i do a make migrations

473.36

let's make sure i don't nope no changes

476.56

i don't think i have to migrate

482.8

no migrations to apply oh

488.639

let's do can i do all

493.44

oh it says none

496.879

so let's just make sure one more time so

498.96

i do dp project

500.24

yeah there's no migration surprise okay

503.44

so let's go take a look back at my query

506.639

uh let's see so it says

510.16

unknown column t

513.839

at time frame

518.32

so if we describe dp project

521.839

underscore task

526

oh it's time frame id my mistake so we

528.8

need to do the time frame id

538.56

and so now we see it's starting to get a

540.399

little bit long but we have

542.32

here is a row where we're going to see

545.519

that this has got january

547.839

oh and there's no tasks tied to

551.279

uh each of those do i have no tasks to

553.76

that let's create some tasks on that guy

556.16

so if i go back to oh i should have it

559.279

oops let's run the server

565.6

okay so running the server

568.8

if i look at here i should have goals i

572.56

should have

573.44

multiple goals uh and this is the time

577.6

frame so

578.48

if i do

581.76

oh and let me actually pull this out

586.56

since i'm sort of working on this on the

588.64

fly

589.6

this is going to be here let's just do

592.399

it like this for now

596.56

uh so we want to left join task on its

599.68

time frame id

600.8

equals the primary id

604.16

oh uh oh no i'm sorry this is not

607.04

actually off of the time frame off the

608.8

parent this is actually

610.079

off of he's actually going to be the

612.16

time frame id equals

614

in this case 1 as well

617.2

because i'm actually i want the tasks

620.16

for this

621.2

this guy not these inner guys

625.92

so now if i do that i'm going to see

628.959

let's see so let's do this one so now i

631.6

see a much longer one

632.8

and i can see i'm going to start i'm

634.56

seeing duplicates all over the place

636.079

because i'm doing all these left joins

637.68

so this goes back to what we

639.2

saw last time where we're going to do

644.839

um

647.68

there we go where one of the things we

649.36

did is we started to do

650.88

group by

654.079

so where is that at

657.2

so now what we want to do is take in our

660.079

long query wherever that went

661.839

let's just do this i think i missed it

664.399

there it is

665.279

okay so for here now there's two things

667.12

we're gonna group by this can become a

668.64

little bit

670.16

annoying so we're gonna go group by

672.72

first

673.76

let's group by um

678.48

how do we want to do this one oh well

680.16

first let's do tf.star

682.76

tf1.star so we're going to take

684.399

everything from here so let's do that

686.839

first

688.079

and that's going to clean it up a little

691.519

because we're going to start seeing

692.64

where our duplicates exist

694.56

so see we can see a bunch of duplicates

696.64

here

700.32

so now that means

703.6

darn it let's come back over here to our

705.36

view let's so the first thing we're

707.44

going to group by is we've got two

708.56

things we need to group by we need to

709.92

group by

711.44

the uh

714.72

tf dot parent id so we're going to group

717.68

by actually

718.56

we're in group by tf2 dot

723.04

id

729.44

and so if we group by the tf2 dot id

733.36

uh here

737.839

did i yeah i get my group yeah i got it

740.72

so now

741.68

we're gonna see only second quarter

745.2

goals plus the three first quarter

746.8

because remember

749.36

we have what we're going to see is we

750.72

have three goals for a period

754.079

so second quarter goal shows up once but

756.639

then we're going to see

757.519

first quarter because we have three

758.639

tasks so now we have to

761.92

uh and with that that means we want to

764.32

uh whoops

765.2

let's go reviews and then here the first

768.24

things we're going to do is we're going

769.2

to do

769.6

count um

773.839

so we have two counts one is going to be

776.8

uh

779.6

children and the

782.72

other is going to be tasks

790

so we need to group by tf2 id and we

792.24

also need to group

793.839

by the task dot id

798

so we group by task id we're going to

799.519

see it's not really going to change

801.839

but we are going to count so children we

803.44

need to count the tf2.id

811.44

and i think we can do this and if we

813.68

count the

814.48

t dot id we're gonna get tasks let's see

816.72

what we get here

820.639

if we go back over here

825.199

so we're not getting that one right so

826.639

we missed something so let's go back to

828.399

this so we count that we're going to

830

group by that

833.199

may have to group this twice because it

834.8

may be a little bit different

837.76

oh that is it so we have to gr we have

839.68

to do this twice now so this one becomes

842.72

a little bit more complicated

848.32

so we actually have to do

853.12

well let's do the children first

857.36

and then we're going to come back

858.16

because this is going to actually be

860.48

a little more complicated

867.76

which may take us into so we're probably

869.519

not that's going to be a two-parter

870.8

because okay so the first thing we want

871.92

to do is we want to get our children and

873.199

then that's going to be

874.24

based off of tf2

877.279

so if we do that we go over here

882.839

well

884.959

oh did i lose i lost my where somewhere

887.839

in there

889.04

uh whoops views

892.16

here it is uh so where

896.079

so if i do that

899.36

i think that's going to give me what i

900.56

need

902.56

oh now i got a count

908.24

oh did i group that right let's go look

910

back at my v

911.36

select so i'm going to select star

916.8

group by that

921.68

so tf1 oh that needs to be count

931.839

where tf1 equals that

940.399

let's see i think that will get us there

946

no we're not quite there because it's

947.279

not counting us so we're not grouping

949.44

something right

952.959

so if we look here we're going to select

954.56

tf1

956.8

we're going to left join on the time

958.72

frame

961.68

on tf2 dot parent id equals that so

964.639

maybe we can count tf

967.12

i don't want to group by tf 2 on a group

969.44

by tf1

971.199

and then this is going to be tf1.star

974.8

count

980.399

and this is going to be children

987.399

tf2.id so now

992.16

we should see it there we go so now we

995.68

see our children

996.56

so now we're gonna since we're doing two

998.32

groupings this is kind of we're gonna

999.759

actually build on this

1001.6

and uh we're gonna take care of that up

1003.04

next episode it's gonna be a nice little

1004.24

two-parter

1005.199

so i'm gonna wrap this one up we've gone

1007.04

a little long i forgot

1008.88

how complicated this one was gonna be

1010.56

that's what happens when you do stuff on

1012

the fly

1013.199

so uh we'll come back around to it but

1016.079

until then

1016.88

go out have yourself a great day a great

1019.04

week and we will talk to you

1021.36

next time

1037.76

you