1
00:00:05,350 --> 00:00:08,980
Welcome back, everyone, to this lecture on Django Admin and models.

2
00:00:10,200 --> 00:00:14,990
Well, we're going to do here is explore how to register our models to the Django admin interface.

3
00:00:15,440 --> 00:00:20,540
We're also going to explore the model Adam in a class which gives us additional functionality with the

4
00:00:20,540 --> 00:00:22,940
fields presented in the admin interface.

5
00:00:23,480 --> 00:00:26,150
Let's begin by going back to our code editor.

6
00:00:26,990 --> 00:00:27,230
All right.

7
00:00:27,260 --> 00:00:29,780
Here I am back inside a visual studio code.

8
00:00:30,140 --> 00:00:32,780
Now you will notice right now I have the views.

9
00:00:33,020 --> 00:00:40,400
And remember, we have our car model every time you create a new application or Django app with the

10
00:00:40,400 --> 00:00:41,360
start up command.

11
00:00:41,810 --> 00:00:45,350
You would have noticed there's an admin de py file created for you.

12
00:00:45,710 --> 00:00:48,740
And if you open up that admin the pie, it'll look like this.

13
00:00:48,770 --> 00:00:54,500
It has this quick command import from Django that contrib import admin, which has a bunch of classes

14
00:00:54,500 --> 00:00:55,400
and functionalities.

15
00:00:55,640 --> 00:00:57,590
And then it says, Register your models here.

16
00:00:58,190 --> 00:01:05,570
So we need to do is actually import the models that we wish to register inside the admin interface.

17
00:01:05,960 --> 00:01:10,980
Keep in mind, depending on the website, it may not make sense to register every single model.

18
00:01:11,000 --> 00:01:17,330
It really depends on do I want a super user to be able to see this and have access to it inside the

19
00:01:17,330 --> 00:01:18,830
administration interface?

20
00:01:19,100 --> 00:01:23,990
Or maybe it's just too much information for a particular model, and it's not really worth it to show

21
00:01:23,990 --> 00:01:25,160
it in the admin interface.

22
00:01:25,550 --> 00:01:31,670
Keep in mind, the fact of the model exists actually presents itself the fact that it will be contained

23
00:01:31,670 --> 00:01:35,810
and stored in a database, and you can always change your mind and register your model in the admin

24
00:01:35,810 --> 00:01:39,830
interface when it becomes clear that you need some sort of administration access.

25
00:01:40,250 --> 00:01:45,350
But let's go ahead and begin by connecting Adam in the PI to the models in the cars.

26
00:01:45,950 --> 00:01:47,540
So what do you say is from?

27
00:01:48,110 --> 00:01:56,780
And there's two ways to do this you can say from the app name the models import car or technically speaking,

28
00:01:56,780 --> 00:02:03,410
you could also just say from the model's import car since you don't actually need to specify cars.

29
00:02:03,620 --> 00:02:09,460
Since adamant that PI isn't the same folder as models that pay by convention on the Django website,

30
00:02:09,470 --> 00:02:16,340
you'll see this used from cars that models import car essentially from app that models import that particular

31
00:02:16,340 --> 00:02:16,620
model.

32
00:02:16,640 --> 00:02:22,880
So, OK, keep this convention because that's what they do inside the documentation, then to register

33
00:02:22,880 --> 00:02:23,360
your model.

34
00:02:23,690 --> 00:02:26,420
The simple way to do this is by simply saying Amen.

35
00:02:26,900 --> 00:02:27,770
That's right.

36
00:02:28,990 --> 00:02:34,990
The register essentially saying wrap the administration site and register and then just pass on the

37
00:02:34,990 --> 00:02:35,890
model to register.

38
00:02:36,460 --> 00:02:38,230
And that's it in a single line of code.

39
00:02:38,560 --> 00:02:42,820
You went ahead and registered that model to appear now in the administration view.

40
00:02:43,210 --> 00:02:44,830
Let's bring up that Adam interview now.

41
00:02:46,660 --> 00:02:54,310
So I'm going to bring in the abdomen of you here, and you'll now notice if I refresh, I get cars so

42
00:02:54,310 --> 00:02:58,720
I can see cars select that they'll take you to the car's administration page.

43
00:02:59,110 --> 00:03:03,280
Cars here, this is the actual application cars and then cars here.

44
00:03:03,460 --> 00:03:04,930
That's the car's model.

45
00:03:04,960 --> 00:03:06,280
And then I can see cars.

46
00:03:06,280 --> 00:03:09,070
If Ford cars, Tesla Car is Toyota.

47
00:03:09,490 --> 00:03:15,550
Notice this is the string representation of the actual car that we overrule in our STR method.

48
00:03:16,000 --> 00:03:18,040
So what you're seeing right now, car is Ford.

49
00:03:18,100 --> 00:03:19,180
Twenty twenty three.

50
00:03:19,720 --> 00:03:24,550
That is this car is brand twenty twenty three year.

51
00:03:24,790 --> 00:03:29,380
So if you wanted it to display something different, you could return another custom string.

52
00:03:29,770 --> 00:03:30,700
So just keep that in mind.

53
00:03:30,730 --> 00:03:32,740
That's what jingo will show you by default.

54
00:03:33,100 --> 00:03:39,490
And then you can actually click on any of these instances and actually begin to update or maybe change

55
00:03:39,490 --> 00:03:40,600
something notice.

56
00:03:40,600 --> 00:03:46,510
Depending on the actual type of field, you may have limited options like I can't now just pass in a

57
00:03:46,690 --> 00:03:49,750
something that's not an integer because year expects an integer.

58
00:03:50,170 --> 00:03:52,270
So maybe I accidentally made a mistake.

59
00:03:52,270 --> 00:03:56,110
And this Ford wasn't actually twenty twenty three, it was twenty twenty two.

60
00:03:56,500 --> 00:04:02,650
What I could do is make that change and then save it, and I've done that from the administration view

61
00:04:03,010 --> 00:04:08,560
because maybe that's the sort of change I don't want to have available to something on the client side.

62
00:04:09,310 --> 00:04:14,770
Maybe I want clients to be able to add, delete or list cars, but I don't want them to be able to update

63
00:04:14,770 --> 00:04:14,920
it.

64
00:04:14,920 --> 00:04:18,190
So I only allow that for administration for some reason.

65
00:04:18,550 --> 00:04:23,950
Again, you can always add new cars as well, so I could click here on that car and I can add a new

66
00:04:23,950 --> 00:04:27,550
car so I can add another Tesla here and then I could make it.

67
00:04:27,550 --> 00:04:29,660
In 1990, Tesla saved that.

68
00:04:29,680 --> 00:04:30,580
And there you go.

69
00:04:31,150 --> 00:04:36,640
And then you can also notice I can select multiples of these and then I can also delete those as well.

70
00:04:37,000 --> 00:04:38,560
I won't do that right now.

71
00:04:38,620 --> 00:04:43,610
The other thing I wanted to point out here is on the one that we just updated car is Ford.

72
00:04:43,660 --> 00:04:44,460
Twenty twenty two.

73
00:04:44,860 --> 00:04:49,680
You can actually check out the history and see who actually did some sort of action to this.

74
00:04:49,750 --> 00:04:53,260
So you notice it will automatically report back on a date and time.

75
00:04:53,530 --> 00:04:57,370
This particular administration user went ahead and changed the year.

76
00:04:57,430 --> 00:05:03,670
So super cool that is done automatically for us, just based on the fact that we did one line of code

77
00:05:04,120 --> 00:05:07,210
here on the fact that we just registered that.

78
00:05:07,210 --> 00:05:11,350
So all that functionality is built into Django automatically for you.

79
00:05:11,860 --> 00:05:16,780
The thing to note here, that's a little confusing based off the choice of my model name is the fact

80
00:05:16,780 --> 00:05:20,150
that we have cars, cars, so I just want to make it clear.

81
00:05:20,620 --> 00:05:21,580
Cars here.

82
00:05:21,610 --> 00:05:23,140
That's the application name.

83
00:05:23,800 --> 00:05:30,640
Cars is actually the model name, and then Django automatically adds an s to basically signify, Oh,

84
00:05:30,640 --> 00:05:35,180
by the way, this is where you're going to see all the instances of a car object.

85
00:05:35,200 --> 00:05:37,390
So cars, so first one's application.

86
00:05:37,720 --> 00:05:39,240
Second one is going to be your model.

87
00:05:39,250 --> 00:05:41,500
And then here's an instance of that model.

88
00:05:41,680 --> 00:05:43,210
And then here's the history of that.

89
00:05:44,420 --> 00:05:50,750
Now, what I want to explore with you is the model admin object class, so I'm going to go back home

90
00:05:50,750 --> 00:05:54,860
here at the jingo administration and there's actually a list here of the recent actions.

91
00:05:54,860 --> 00:05:58,760
So anybody logging in, that's a super user can see, OK, what's changed recently?

92
00:05:59,150 --> 00:06:02,690
But what I'm going to do is explore the model admin object.

93
00:06:02,990 --> 00:06:07,970
In fact, if I bring in the documentation here, you can see it's a model admin object and they actually

94
00:06:07,970 --> 00:06:09,730
tell you a little bit.

95
00:06:09,770 --> 00:06:12,110
Hey, by the way, maybe you don't need the model admin object.

96
00:06:12,110 --> 00:06:13,790
You just can do a single line register.

97
00:06:14,120 --> 00:06:20,930
But if you want a lot more flexibility on the exact way that the administration is set out for any particular

98
00:06:20,930 --> 00:06:28,790
model object, you can create this model admin and connect the actual model to the actual admin class

99
00:06:29,060 --> 00:06:29,990
version of that model.

100
00:06:30,320 --> 00:06:36,290
So I would recommend for beginners that they don't really need to worry about this level of sophistication

101
00:06:36,290 --> 00:06:41,240
or detail within the actual admin view, but I do want you to be aware of it.

102
00:06:41,630 --> 00:06:48,110
So what we're going to do here is I'm again going to create a new class inside admin that.

103
00:06:49,070 --> 00:06:51,110
And I'm going to call this my car.

104
00:06:53,460 --> 00:06:54,060
Admin.

105
00:06:55,290 --> 00:07:01,080
And this is going to inherit from admin, and then I'm going to say model admin.

106
00:07:03,350 --> 00:07:08,180
Now, theoretically, you could call this whatever you want, but I would recommend that you follow

107
00:07:08,180 --> 00:07:13,310
the convention of model name and then Ataman and then here we're just going to say pass.

108
00:07:13,550 --> 00:07:16,820
Later on, we will fill this out more and we're going to say Amen.

109
00:07:17,300 --> 00:07:17,930
That's right.

110
00:07:19,110 --> 00:07:25,380
Register and then this registration actually needs to connect this admin class we just made with the

111
00:07:25,380 --> 00:07:26,310
car model.

112
00:07:26,520 --> 00:07:32,220
So you pass in the original model first car and then you say, OK, the actual admin registration for

113
00:07:32,220 --> 00:07:35,010
it, though, is car admin class.

114
00:07:35,400 --> 00:07:39,630
Notice this is essentially the exact same thing we did before, except instead of just using the default

115
00:07:39,960 --> 00:07:45,870
model admin, I'm not using my own custom car admin, so you can think of this as essentially beginning

116
00:07:45,870 --> 00:07:48,450
to extend from default model admin.

117
00:07:48,870 --> 00:07:53,490
And if you do that, change and refresh your administration page, everything's still going to look

118
00:07:53,490 --> 00:07:53,910
the same.

119
00:07:54,420 --> 00:07:58,290
So what can we actually do with the admin class?

120
00:07:58,380 --> 00:08:03,030
This allows you to really customize the administration form with a lot more detail.

121
00:08:03,750 --> 00:08:07,860
For example, one of the fields or attributes you can overwrite is fields.

122
00:08:08,820 --> 00:08:14,700
So when you're actually creating your version of the car admin, you can actually specify the order

123
00:08:14,700 --> 00:08:15,420
of the fields.

124
00:08:15,810 --> 00:08:20,970
And if I bring in the site right now, I'm going to comment this out and I'll actually save that change

125
00:08:20,970 --> 00:08:21,180
changer.

126
00:08:21,750 --> 00:08:29,010
If I bring in the site right now and go to the car model and check out the fields on this notice, brand

127
00:08:29,010 --> 00:08:31,650
comes up first and here comes up second.

128
00:08:32,100 --> 00:08:38,159
But maybe I have dozens and dozens of fields and I actually want a certain order to show up because

129
00:08:38,159 --> 00:08:40,760
that's the order that I am most likely to update.

130
00:08:40,770 --> 00:08:42,179
That could actually be really helpful.

131
00:08:42,600 --> 00:08:45,600
So what I could do is switch out the order of these fields.

132
00:08:45,960 --> 00:08:50,940
Notice that this is essentially a Django form being created automatically for us based off the model

133
00:08:50,940 --> 00:08:51,570
attributes.

134
00:08:52,020 --> 00:08:59,220
So to specify the order of the fields you grab, the fields attribute inside your new custom admin for

135
00:08:59,220 --> 00:09:04,620
this model and then specify and strings the actual field names you want.

136
00:09:05,040 --> 00:09:07,830
So, for example, remember it was brand and year.

137
00:09:08,100 --> 00:09:11,250
So if we come to models, we have brand and year.

138
00:09:12,030 --> 00:09:16,710
So what I'm going to do is come back to that pie and say the following Let's say now, year.

139
00:09:17,930 --> 00:09:22,130
And brand, then I'm going to save that change.

140
00:09:22,490 --> 00:09:24,200
It's going to reload everything.

141
00:09:24,710 --> 00:09:30,620
Let's come back to Django Adminn home refresh that so I can then see those changes come back to cars

142
00:09:31,310 --> 00:09:35,600
and one of these and I say it's reversed now it's the year and then it's the brand.

143
00:09:36,050 --> 00:09:39,800
So obviously not a big deal to fields to happen to switch the order.

144
00:09:40,100 --> 00:09:43,400
But clearly with many fields, this would actually be super useful.

145
00:09:43,970 --> 00:09:50,690
Well, we can also do is actually begin to segment and split up the fields into what are known as field

146
00:09:50,690 --> 00:09:51,320
sets.

147
00:09:51,710 --> 00:09:56,570
So then you can actually label different parts of the actual fields.

148
00:09:56,930 --> 00:10:02,600
That way, it's even more easy to use or easier to use for someone who's a super user.

149
00:10:02,990 --> 00:10:06,890
The way that works is instead of defining fields, you define the attribute.

150
00:10:07,950 --> 00:10:09,150
Field sets.

151
00:10:09,870 --> 00:10:18,870
And this is going to be a list of tuple pairs, so we have a list like this and it's tuple pairs where

152
00:10:18,900 --> 00:10:25,380
you're going to give the string name of what you actually want that field to throw that field set to

153
00:10:25,380 --> 00:10:25,860
be called.

154
00:10:26,280 --> 00:10:29,280
So let's say I want all time.

155
00:10:30,280 --> 00:10:34,330
Information, I want that to actually be a section of my form.

156
00:10:35,050 --> 00:10:41,320
And then I have to define what is going to be in dictionary form, the fields associated with that.

157
00:10:41,830 --> 00:10:43,720
So here we have dictionary and we say fields.

158
00:10:44,910 --> 00:10:53,130
Colin and the you the fields, so for example, I can say, yea, let's imagine I also had a day attribute

159
00:10:53,130 --> 00:10:58,110
in the model, so maybe I didn't just have a year but also had day, etc. I could keep adding to that

160
00:10:58,110 --> 00:10:58,380
list.

161
00:10:58,420 --> 00:11:00,750
I could say here, OK, also add in the day attribute.

162
00:11:01,800 --> 00:11:07,250
So not super useful for two fields, but again, we're creating field sets.

163
00:11:07,380 --> 00:11:12,480
The way it works is you have a tuple where that set is going to be labeled time information.

164
00:11:12,810 --> 00:11:18,300
Then you create a dictionary with the key fields and then pass on a list of the attributes.

165
00:11:18,330 --> 00:11:22,200
So let's create another tuple pairing because we still have one field.

166
00:11:22,200 --> 00:11:29,370
Remember, we have brand so that I can create and there are tuple here, and let's call this car information.

167
00:11:31,400 --> 00:11:33,110
And we again say comma here.

168
00:11:33,290 --> 00:11:36,880
And then we say dictionary key is fields, colon.

169
00:11:37,250 --> 00:11:38,570
And let's pass on a list now.

170
00:11:38,990 --> 00:11:40,520
And now we're going to put in brand there.

171
00:11:41,880 --> 00:11:43,290
And then save those changes.

172
00:11:43,740 --> 00:11:51,330
So what this is going to do is it's going to reformat the actual fields in the form to have sets, and

173
00:11:51,330 --> 00:11:53,130
that way it can have sections essentially.

174
00:11:53,550 --> 00:11:56,580
So I come back to I'm going to refresh to make sure those changes took place.

175
00:11:56,850 --> 00:11:58,710
Make sure you're saving your admin, not pie file.

176
00:11:58,710 --> 00:12:00,380
When you make those changes, come back.

177
00:12:00,400 --> 00:12:05,460
The cars come back to cars Tesla 1990, and now you'll notice these blue bars.

178
00:12:05,670 --> 00:12:07,830
Time information, car information.

179
00:12:08,280 --> 00:12:10,320
Those are the actual field sets.

180
00:12:10,770 --> 00:12:13,230
So just to kind of map it to what we just did.

181
00:12:13,650 --> 00:12:20,760
We can see here I have time information as that string in the tuple and then the fields that are associated

182
00:12:20,760 --> 00:12:21,630
with that.

183
00:12:22,000 --> 00:12:24,180
And the way that works is, again, fields.

184
00:12:24,180 --> 00:12:25,950
And then you pass in, you're here.

185
00:12:26,400 --> 00:12:32,460
So that allows you again to further customize the way this form is actually showing up for the super

186
00:12:32,460 --> 00:12:32,820
user.

187
00:12:35,290 --> 00:12:41,410
Now we're going to end our discussion of the admin panel and admin view at this for now.

188
00:12:41,440 --> 00:12:47,020
So again, we really just wanted to make sure you understood how to create your own admin class that

189
00:12:47,020 --> 00:12:52,210
extends from admin, that model admin and that you were able to register that and then possibly maybe

190
00:12:52,210 --> 00:12:54,580
edit the field order or edit the field sets.

191
00:12:55,030 --> 00:13:00,280
We're going to revisit in different projects the actual administration view and do a couple more customizations.

192
00:13:00,610 --> 00:13:05,740
But I want you to keep in mind that this is actually a extremely powerful class, and you can make this

193
00:13:05,740 --> 00:13:11,480
administration view look and be stylized however you want if you are interested in doing that.

194
00:13:11,500 --> 00:13:15,550
Again, I would really focus on the other aspects of your site before the administration site.

195
00:13:15,730 --> 00:13:19,290
This is maybe one of the last things you start editing, but if you are interested in that?

196
00:13:19,360 --> 00:13:24,130
Go to the documentation on the Django admin site portion of the documentation.

197
00:13:24,520 --> 00:13:30,850
And if you scroll down, it will eventually have this section on model admin objects and it will show

198
00:13:30,850 --> 00:13:34,270
you all the different methods and things you can override.

199
00:13:34,570 --> 00:13:37,180
They'll also talk about things like Maybe you want to override the template.

200
00:13:37,330 --> 00:13:40,360
Again, not something I would suggest unless that was super important for you.

201
00:13:40,660 --> 00:13:45,430
But you can come here to that section on overwriting admin templates, and it will essentially tell

202
00:13:45,430 --> 00:13:48,490
you, Hey, let's go back there overwriting.

203
00:13:50,180 --> 00:13:51,320
So if you come back here.

204
00:13:52,290 --> 00:13:57,240
Overriding admin templates, it'll tell you, hey, you didn't go to this actual contrib admin templates

205
00:13:57,240 --> 00:14:01,740
admin and then you can open up and begin to actually change the HTML.

206
00:14:02,010 --> 00:14:05,940
There's actually a bunch of the first stimulus you can change, but you should also note that you probably

207
00:14:05,940 --> 00:14:09,390
want to overwrite rather than completely replace those actual template files.

208
00:14:09,690 --> 00:14:13,590
But it does show you the way it basically works here on what they're showing you.

209
00:14:14,250 --> 00:14:14,580
OK?

210
00:14:14,940 --> 00:14:20,970
And there's a bunch of HTML files associated with the fact that the administration has multiple views

211
00:14:20,970 --> 00:14:22,590
and multiple templates for those views.

212
00:14:23,010 --> 00:14:28,710
Again, it's a super powerful tool, but I would recommend this is something you explore towards the

213
00:14:28,710 --> 00:14:34,650
end of your Django journey because the defaults are already so darn good with what Django provides for

214
00:14:34,650 --> 00:14:38,970
you, and you should really be focusing on the other aspects of functionality on your website.

215
00:14:39,300 --> 00:14:44,160
So we'll be revisiting other aspects of Django admin, but for right now, the main take away is the

216
00:14:44,160 --> 00:14:51,180
fact that we now have the ability to register models and view them within our admin inside the super

217
00:14:51,180 --> 00:14:51,720
user view.

218
00:14:52,260 --> 00:14:52,560
OK.

219
00:14:52,860 --> 00:14:53,290
That's it.

220
00:14:53,310 --> 00:14:54,270
I'll see you at the next lecture.

