📺 Develpreneur YouTube Episode

Video + transcript

Learn Python And Django Day 41

2021-03-18 •Youtube

Detailed Notes

This is a 2nd 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 40 as well): https://github.com/robbroadhead/develpreneur-pythontutorial

Transcript Text
[Music]
okay welcome back
and we are continuing going into some
sort of complex
uh raw queries we're getting a little
drifting i apologize i guess in a sense
we're drifting into some more complex
sql which is not really in our
general django python tutorials but
it's something that's going to be useful
for us so
last time around we were able to get uh
we're looking at building out some
information
here actually i'm sorry here at the
roadmap levels i want to be able to see
four time frames what are the child time
frames and tasks
doing so we've got this time we've got
the child time frames
if we look at this guy he's got annual
and professional goals
uh oh i'm looking at the road map i'm
sorry here
so this is the one i'm looking at and
what i need to do
uh did i get that oh so i probably want
to i'm going to probably want to pull
these guys
actually here so let's actually look up
uh
well we'll fix that putting in the wrong
place but that's okay
uh based on this whoops
so oh here we go roadmap has seven
children
which is interesting that i've managed
to somehow break
that one because now he's looking at all
of the children
in there so interesting and oh because
they're all under that road map not
under
so it's under all of these time frames
because i have here i have a child time
frame
and i have some other time frames
oh anyways so right now what we're
getting is we're getting oops we're here
we're getting these two time frames for
this one
and we have counted the children but now
we need to count the tasks
and what we end up having to do here is
we're going to end up taking
this big thing and we're going to wrap
it
we're going to call it source i guess
we're going to call it
uh we'll call it main
and then what we're going to end up
doing is we're going to select star
from that i'll actually do
i'll do main.star
from this query from this query which is
going to give us
essentially this information
and then what we need to do
um actually wait i want to do it with
the group by
so i'm going to get all the way here and
then
i'm gonna have to left join on
tp project whoops
underscore task i'm gonna call it t
on t dot and what do we call that uh
time
frame id
equals main dot
what are we calling it it's going to be
main
oh it's actual id so we can actually
keep it here
so we're gonna call it
uh because it comes back here so main
dot
oh we may not have to do that so we need
to do it here
i think we can do that so let's do this
t dot star so we're going to bring all
the tasks in for each of those
so if we do that let's see what we get
it may not take it okay
generally speaking we need to take a
select star for well let's do this
let's take it in pieces
so we'll come back first we're going to
do select star from
uh the time frame
and then we would left join him on oh
actually we can just do that
and actually i think we can do it here
where he equals tf1.id
and then we're going to have our where
and if we look at that
actually we do want no because we want
this to be and
uh time frame equals one
let's just take that for now
uh did we
oh
data time frame id
oh let's look at what we did we typed
something here
oh my mistake
so take him and this is going to give us
our tasks
or not
uh where tf one not parent id equals
this
oh it's not
this is going to be t f1 dot
uh time frame
id oh alright got it
private state so if i go here
uh what am i missing
on that oh
i have that see on
let's join that on that there we go
foolish sequel issue okay so there's all
my tasks
within that time frame
and so i'm seeing this here
oh it's because it goes up to the road
map so it's not where the road map
is oh yes
because this can be all tasks under a
road map
for a given one
oh that should work because i'm going to
get everybody
where the time frame and actually i
guess all the tasks are probably tied up
to this guy right now
if i look at my i don't know i have task
of task lists here we go
yes i'm going to actually see all of
these guys i'll see them twice because
i'm going to see them
based on
the id
so now what i need to do is i need to
find a way to left join these things
and what i probably want to do let's see
is i'm based on here so if i look at a
roadmap
i'm not going to bother with it but i am
going to do within here
so for this given id
so this is actually and this is actually
in roadmap so we're going to fix that
that's we're going to move our query so
given an id
of this guy which is a time frame
i want to do my tasks which should be
three that are tied to that task
alone so let's go back and look at this
so if i select from the time frame
oh that's right i had where
that was everybody
now i build this back and this is going
to look a little better clean this up
so now i'm just seeing uh first quarter
i've got
three tasks second quarter i've got
three tasks so they're not
actually assigning right one i've
actually because i've got these three
tasks and it's
by a quarter
i'm trying to figure out because i'm
doing left join task on that
so it's everywhere that the time frame
id so these guys didn't get their time
frame ids built right i don't think
oh i bet i have oh maybe i do do i have
tasks
so he's got
uh let's see so if i go back and look
here if i look at second quarter goals i
don't have any tasks
if i look at first quarter goals i do
have tasks
uh create application version one for
example
oh but that's not what i'm looking at i
should see my mistake i should see up
here
for this one i should see these three so
i see those three but i see them across
the two
time frames so let's go look at our
query here
let's see so if i go here i'm going to
select star from time frame
i'm going to left join the task on that
where
time frame parent id equals one but it's
actually
uh where
so let's look at this real quick looking
at my results i really need
where
uh if i look at those tasks
so first quarter if i look at these guys
what's the differentiating that is
this id
so i don't think he's coming up right so
the task should be
oh that's what it is i'm looking at the
task based on the children and i don't
want it there i want it actually here
uh so i want the time frame id
equals to one i don't know
oh yeah we're paranoid equals one
no this is where time frame dot id
one so i can actually do it
uh t dot time frame id equals
tf1.id so i can do it like this
my mistake and so now whoop now i'm
gonna see my three so i've got my three
tasks
whoopsie so let's flip over here if i
look real quick
at this i can see so this one gives me
my children this one gives me my tasks
or my
test child thing so what i can do here
is i'm going to make this my main
and then what i need to do is
left join
actually it's going to be an inner join
i believe
well we can probably do a left joint
it's going to get really complicated
really fast
so i'm going to left join this guy
uh this will be goals on
goals dot id
equals
main dot id let's see how this works out
and so this one is just going to be
um i want to do
uh this is going to be t f dot id
as id comma well we'll leave it as id
and then we're going to do the second
which is going to be
t dot id is going to be tid
and so uh this is gonna be
t let's do that let's do main dot star
comma uh the second guy
goals and we'll do goals.star
and then we're gonna have to do the wear
again so where
[Music]
main dot id
equals one because this one i'm gonna
have our guy again
uh
i don't think i need that let's see
did we get it
okay it's an empty set
so let's go look at our two queries so
our first query
gives us this
and we have children so we have a period
we have an id
and then what we need to do
oh he's actually not going to be in
there so we have to within
each of these oh goals is actually a
separate count
by mistake all right so then we're going
to do this
okay i'm jumping ahead a little bit
because there's a lot of complex
stuff there that was in the sql that i
wanted to work through
what we ended up with is we do have a
nice little query here
that gives us a number of children so we
need to do
is uh we plug that in
and uh it ends up we're tying it up to
the roadmap
so it's going to be where the roadmap id
equals
this value that we're going to send in
it's just something a little new so
we've got our raw query
we go ahead and we have a we're going to
tag our or uh
our order by into the sql
and then that allows us to do our order
by
which we'll see here by name
and then what we do is we do within this
raw you do comma and then you're going
to give it
an array of values we just have one
so it's going to be id and it's going to
plug id into the percent s
if we had a second one like we could do
percent s and then we could do
maybe another percent s here then we do
id comma whatever that second value is
so now with that and our query
then all we need to do is update
our uh oops is update our roadmap
list i'm sorry roadmap edit
and we're going to add children and
we're going to take this extra
column we just add which is children
itself uh we called it children you see
here
and we're going to do that to display so
now we're going to see this query
i'm going to see that pop over on this
screen and we can see
that we have two children
actually let's change that a little bit
and we're gonna say
has
just to make it a little cleaner has
that children
and we could do tasks uh but we'll wait
let's see we'll just keep it as a period
so now we'll see is how many children it
has
and then we're going to go into this and
i don't know if i want to deal with
goals or not we'll sort of see how that
looks
uh but it does mean we need to update
this one essentially the same
and so we'll come back around next time
and we're going to do a couple of those
things where i do that and actually
probably going to change up a little bit
of our navigation so we can add goals
and
time frames and do it in a way that
makes a little more sense
so it's not that painful thing that we
did here where we kept having to do back
arrows and stuff like that so
we're continuing to clean it up getting
there it's almost done
as far as general functionality and then
we'll be doing more user experience
stuff but
that's it for this part of raw queries
and using some parameters as always out
there have yourself a great day
a great week and we will talk to you
next
you
Transcript Segments
0.46

[Music]

26.4

okay welcome back

27.76

and we are continuing going into some

29.679

sort of complex

30.8

uh raw queries we're getting a little

32.48

drifting i apologize i guess in a sense

35.28

we're drifting into some more complex

37.04

sql which is not really in our

39.36

general django python tutorials but

43.12

it's something that's going to be useful

44.32

for us so

46.399

last time around we were able to get uh

49.44

we're looking at building out some

50.879

information

52

here actually i'm sorry here at the

54.079

roadmap levels i want to be able to see

56.719

four time frames what are the child time

58.719

frames and tasks

61.039

doing so we've got this time we've got

63.039

the child time frames

64.32

if we look at this guy he's got annual

66.72

and professional goals

70

uh oh i'm looking at the road map i'm

72.479

sorry here

74.72

so this is the one i'm looking at and

76.32

what i need to do

78.56

uh did i get that oh so i probably want

82

to i'm going to probably want to pull

83.04

these guys

83.68

actually here so let's actually look up

86.4

uh

87.119

well we'll fix that putting in the wrong

89.52

place but that's okay

91.84

uh based on this whoops

94.96

so oh here we go roadmap has seven

98.84

children

101.2

which is interesting that i've managed

103.119

to somehow break

106.079

that one because now he's looking at all

108.159

of the children

110

in there so interesting and oh because

112

they're all under that road map not

113.92

under

115.36

so it's under all of these time frames

117.2

because i have here i have a child time

118.719

frame

119.6

and i have some other time frames

123.04

oh anyways so right now what we're

125.52

getting is we're getting oops we're here

128.239

we're getting these two time frames for

129.92

this one

131.28

and we have counted the children but now

133.44

we need to count the tasks

136.08

and what we end up having to do here is

138

we're going to end up taking

139.36

this big thing and we're going to wrap

143.12

it

144.16

we're going to call it source i guess

146.4

we're going to call it

147.36

uh we'll call it main

153.2

and then what we're going to end up

154.16

doing is we're going to select star

156.879

from that i'll actually do

160.08

i'll do main.star

163.12

from this query from this query which is

165.84

going to give us

166.72

essentially this information

170.8

and then what we need to do

177.44

um actually wait i want to do it with

179.76

the group by

182

so i'm going to get all the way here and

184.64

then

185.04

i'm gonna have to left join on

188.879

tp project whoops

193.84

underscore task i'm gonna call it t

197.599

on t dot and what do we call that uh

200.48

time

200.959

frame id

204.159

equals main dot

208.4

what are we calling it it's going to be

210.84

main

213.599

oh it's actual id so we can actually

216.159

keep it here

219.36

so we're gonna call it

223.519

uh because it comes back here so main

227.04

dot

232.879

oh we may not have to do that so we need

234.48

to do it here

236.799

i think we can do that so let's do this

240.879

t dot star so we're going to bring all

242.319

the tasks in for each of those

245.36

so if we do that let's see what we get

250.56

it may not take it okay

254.159

generally speaking we need to take a

256.239

select star for well let's do this

258.4

let's take it in pieces

261.6

so we'll come back first we're going to

263.04

do select star from

267.12

uh the time frame

271.919

and then we would left join him on oh

275.12

actually we can just do that

279.759

and actually i think we can do it here

281.28

where he equals tf1.id

288.4

and then we're going to have our where

295.199

and if we look at that

301.36

actually we do want no because we want

302.88

this to be and

304.4

uh time frame equals one

309.6

let's just take that for now

315.759

uh did we

322.84

oh

326.16

data time frame id

332.08

oh let's look at what we did we typed

333.919

something here

335.36

oh my mistake

341.039

so take him and this is going to give us

343.28

our tasks

347.6

or not

351.199

uh where tf one not parent id equals

353.68

this

354.32

oh it's not

366.639

this is going to be t f1 dot

370.96

uh time frame

374.08

id oh alright got it

378.319

private state so if i go here

381.52

uh what am i missing

386

on that oh

392.319

i have that see on

395.36

let's join that on that there we go

400.88

foolish sequel issue okay so there's all

402.88

my tasks

406.24

within that time frame

410.8

and so i'm seeing this here

419.36

oh it's because it goes up to the road

420.96

map so it's not where the road map

423.68

is oh yes

427.84

because this can be all tasks under a

429.84

road map

431.28

for a given one

436.24

oh that should work because i'm going to

437.68

get everybody

439.44

where the time frame and actually i

441.039

guess all the tasks are probably tied up

442.72

to this guy right now

445.199

if i look at my i don't know i have task

448.08

of task lists here we go

449.599

yes i'm going to actually see all of

450.96

these guys i'll see them twice because

452.72

i'm going to see them

454.4

based on

457.68

the id

460.8

so now what i need to do is i need to

462.479

find a way to left join these things

467.599

and what i probably want to do let's see

474.879

is i'm based on here so if i look at a

477.84

roadmap

478.639

i'm not going to bother with it but i am

480.319

going to do within here

483.44

so for this given id

487.039

so this is actually and this is actually

488.479

in roadmap so we're going to fix that

490.16

that's we're going to move our query so

492.16

given an id

493.599

of this guy which is a time frame

497.919

i want to do my tasks which should be

499.759

three that are tied to that task

502.08

alone so let's go back and look at this

506.8

so if i select from the time frame

511.84

oh that's right i had where

515.36

that was everybody

519.519

now i build this back and this is going

521.039

to look a little better clean this up

525.279

so now i'm just seeing uh first quarter

527.839

i've got

528.56

three tasks second quarter i've got

530.72

three tasks so they're not

531.92

actually assigning right one i've

534

actually because i've got these three

535.04

tasks and it's

535.92

by a quarter

540.24

i'm trying to figure out because i'm

541.2

doing left join task on that

546.64

so it's everywhere that the time frame

548.32

id so these guys didn't get their time

550

frame ids built right i don't think

554.72

oh i bet i have oh maybe i do do i have

556.8

tasks

558.24

so he's got

561.44

uh let's see so if i go back and look

565.36

here if i look at second quarter goals i

568.08

don't have any tasks

569.6

if i look at first quarter goals i do

572.24

have tasks

575.2

uh create application version one for

577.12

example

579.6

oh but that's not what i'm looking at i

581.839

should see my mistake i should see up

584.08

here

586.399

for this one i should see these three so

588.72

i see those three but i see them across

590.32

the two

591.2

time frames so let's go look at our

593.12

query here

596.959

let's see so if i go here i'm going to

599.2

select star from time frame

600.88

i'm going to left join the task on that

603.839

where

604.399

time frame parent id equals one but it's

606.56

actually

610.64

uh where

615.2

so let's look at this real quick looking

616.72

at my results i really need

618.399

where

622.56

uh if i look at those tasks

627.68

so first quarter if i look at these guys

629.6

what's the differentiating that is

631.36

this id

638.56

so i don't think he's coming up right so

640.24

the task should be

646.24

oh that's what it is i'm looking at the

649.76

task based on the children and i don't

651.6

want it there i want it actually here

655.519

uh so i want the time frame id

660.16

equals to one i don't know

664.56

oh yeah we're paranoid equals one

669.12

no this is where time frame dot id

674.16

one so i can actually do it

677.68

uh t dot time frame id equals

681.8

tf1.id so i can do it like this

686.72

my mistake and so now whoop now i'm

689.44

gonna see my three so i've got my three

690.959

tasks

693.04

whoopsie so let's flip over here if i

695.519

look real quick

696.8

at this i can see so this one gives me

700.24

my children this one gives me my tasks

703.6

or my

704.48

test child thing so what i can do here

710.88

is i'm going to make this my main

716.959

and then what i need to do is

721.279

left join

724.399

actually it's going to be an inner join

725.76

i believe

730.32

well we can probably do a left joint

731.68

it's going to get really complicated

732.88

really fast

734.079

so i'm going to left join this guy

739.36

uh this will be goals on

744.88

goals dot id

748.32

equals

751.44

main dot id let's see how this works out

758.88

and so this one is just going to be

762.56

um i want to do

769.44

uh this is going to be t f dot id

775.2

as id comma well we'll leave it as id

777.92

and then we're going to do the second

779.12

which is going to be

779.839

t dot id is going to be tid

786.48

and so uh this is gonna be

790.959

t let's do that let's do main dot star

795.519

comma uh the second guy

800.16

goals and we'll do goals.star

811.2

and then we're gonna have to do the wear

812.639

again so where

813.84

[Music]

814.959

main dot id

818.48

equals one because this one i'm gonna

820.959

have our guy again

822.16

uh

826.56

i don't think i need that let's see

829.76

did we get it

834.16

okay it's an empty set

839.839

so let's go look at our two queries so

841.44

our first query

842.88

gives us this

847.12

and we have children so we have a period

849.04

we have an id

850.399

and then what we need to do

855.279

oh he's actually not going to be in

857.44

there so we have to within

858.959

each of these oh goals is actually a

860.959

separate count

862.16

by mistake all right so then we're going

864.959

to do this

867.199

okay i'm jumping ahead a little bit

869.12

because there's a lot of complex

871.76

stuff there that was in the sql that i

873.44

wanted to work through

875.199

what we ended up with is we do have a

878.24

nice little query here

881.199

that gives us a number of children so we

882.88

need to do

884.399

is uh we plug that in

887.44

and uh it ends up we're tying it up to

889.44

the roadmap

890.48

so it's going to be where the roadmap id

892.8

equals

893.92

this value that we're going to send in

895.76

it's just something a little new so

897.12

we've got our raw query

898.959

we go ahead and we have a we're going to

900.639

tag our or uh

902.16

our order by into the sql

906

and then that allows us to do our order

908.56

by

909.44

which we'll see here by name

912.88

and then what we do is we do within this

915.68

raw you do comma and then you're going

917.12

to give it

918.56

an array of values we just have one

921.76

so it's going to be id and it's going to

924.16

plug id into the percent s

925.839

if we had a second one like we could do

927.839

percent s and then we could do

929.04

maybe another percent s here then we do

930.88

id comma whatever that second value is

934.079

so now with that and our query

938.079

then all we need to do is update

943.199

our uh oops is update our roadmap

947.12

list i'm sorry roadmap edit

950.56

and we're going to add children and

952.639

we're going to take this extra

954.079

column we just add which is children

955.68

itself uh we called it children you see

958.24

here

959.279

and we're going to do that to display so

960.639

now we're going to see this query

962.399

i'm going to see that pop over on this

963.92

screen and we can see

966.079

that we have two children

970.079

actually let's change that a little bit

972.48

and we're gonna say

974.72

has

979.199

just to make it a little cleaner has

982

that children

985.6

and we could do tasks uh but we'll wait

991.92

let's see we'll just keep it as a period

996.56

so now we'll see is how many children it

998.399

has

1000.16

and then we're going to go into this and

1003.279

i don't know if i want to deal with

1004.399

goals or not we'll sort of see how that

1005.839

looks

1006.399

uh but it does mean we need to update

1008.72

this one essentially the same

1011.04

and so we'll come back around next time

1012.639

and we're going to do a couple of those

1013.6

things where i do that and actually

1015.6

probably going to change up a little bit

1016.959

of our navigation so we can add goals

1019.68

and

1020.24

time frames and do it in a way that

1022.32

makes a little more sense

1024

so it's not that painful thing that we

1025.679

did here where we kept having to do back

1028.079

arrows and stuff like that so

1029.679

we're continuing to clean it up getting

1031.439

there it's almost done

1034

as far as general functionality and then

1035.439

we'll be doing more user experience

1037.28

stuff but

1038.64

that's it for this part of raw queries

1041.839

and using some parameters as always out

1044.72

there have yourself a great day

1046.079

a great week and we will talk to you

1050.84

next

1064.799

you