1
00:00:05,410 --> 00:00:08,620
Welcome back, everyone, to this lecture on models and databases.

2
00:00:09,790 --> 00:00:16,090
Jingle models are defined inside a jingle app or project inside the model's top pie file, which is

3
00:00:16,090 --> 00:00:18,010
a file we haven't actually played around with yet.

4
00:00:18,730 --> 00:00:24,460
The model's class operates on a system which ends up directly converting the Python based code into

5
00:00:24,460 --> 00:00:26,650
SQL commands that interact with that database.

6
00:00:26,980 --> 00:00:31,300
This makes it much easier to work with the backend database because you just need to worry about writing

7
00:00:31,300 --> 00:00:32,049
Python code.

8
00:00:33,310 --> 00:00:37,600
Now, creating a single model is similar, it's creating a new table in a database.

9
00:00:38,110 --> 00:00:41,050
For example, you can have your database, it's an SQL light.

10
00:00:41,140 --> 00:00:44,380
And then you want to create tables that exist within that database.

11
00:00:44,740 --> 00:00:47,980
For example, let's say we're running a website for a grocery store.

12
00:00:48,310 --> 00:00:51,100
So we have customers or food items or employees.

13
00:00:51,550 --> 00:00:54,970
Later on, we'll also discuss how we can create relationships between these tables.

14
00:00:55,300 --> 00:01:00,010
For example, check if a customer actually bought a particular food item, and then we could make a

15
00:01:00,010 --> 00:01:05,590
new table for something like orders and actually connect a customer to a food item using relationships

16
00:01:05,950 --> 00:01:07,990
one to one one too many and so on.

17
00:01:08,320 --> 00:01:10,770
We'll talk about that more details later on.

18
00:01:12,180 --> 00:01:16,920
For right now, though, each database table has a name and then columns where each column will have

19
00:01:16,920 --> 00:01:18,030
a specific data type.

20
00:01:18,480 --> 00:01:23,940
For example, character strings would be that good data type for names of customers or integers.

21
00:01:23,940 --> 00:01:27,570
Would it be a good data type for ages of customers in years?

22
00:01:28,110 --> 00:01:32,940
So now let's explore how general models works in conjunction with these sort of structures.

23
00:01:34,860 --> 00:01:40,050
So inside of your models, that profile, you're going to have an import line that says something like

24
00:01:40,050 --> 00:01:46,590
from Django, that DB import models and then to create a new model which directly correlates to a new

25
00:01:46,590 --> 00:01:52,650
table in your database, you create a class and then you get to decide what you want to call that particular

26
00:01:52,650 --> 00:01:54,030
model or database.

27
00:01:54,330 --> 00:01:58,740
And then you add in attributes which end up being fields in your model.

28
00:01:59,100 --> 00:02:02,160
The attributes are analogous to columns in the table.

29
00:02:03,680 --> 00:02:08,810
This, in turn, gets automatically converted to some sort of sequel command to the database to actually

30
00:02:08,810 --> 00:02:10,850
create the table so we can see here.

31
00:02:10,880 --> 00:02:16,520
This is direct conversion from your Python code and then this gets turned into SQL code that can then

32
00:02:16,520 --> 00:02:17,760
interact with the database.

33
00:02:17,780 --> 00:02:19,970
Again, you don't need to worry about that bottom.

34
00:02:19,970 --> 00:02:23,720
You just need to worry about the Python code on top, which is much easier to work with.

35
00:02:25,500 --> 00:02:28,650
Now, let's talk about a couple of key concepts for jingle models.

36
00:02:28,950 --> 00:02:32,490
The first one is that it inherits from the built in models class.

37
00:02:32,790 --> 00:02:37,770
So there's a bunch of methods that are available to you right off the bat from inheriting from models

38
00:02:37,770 --> 00:02:38,850
that models class.

39
00:02:39,330 --> 00:02:45,540
It also uses fields to define both data types, and sometimes some data constraints don't get a little

40
00:02:45,540 --> 00:02:50,340
too confused on this because there are actual constraints classes within Django.

41
00:02:50,580 --> 00:02:55,500
But you can think of the arguments for passing into the fields as also defining constraints.

42
00:02:55,860 --> 00:03:01,950
For example, maybe you want to have a particular data type column or field require information like

43
00:03:01,950 --> 00:03:07,710
a user's email address, in which case you can add a not null constraint, not know what would be the

44
00:03:07,710 --> 00:03:09,570
terminology used in sequel.

45
00:03:09,810 --> 00:03:14,760
But later on, you'll see that there's actually easy to use arguments you can pass in during the field

46
00:03:14,760 --> 00:03:16,650
creation to put a constraint on it.

47
00:03:17,100 --> 00:03:21,690
For example, if we look back here, the first name and the last name, we're constrained to have a

48
00:03:21,690 --> 00:03:28,560
max length of 30 characters, and we can see here a character field was chosen for that particular attribute.

49
00:03:30,510 --> 00:03:35,190
Another example could be maybe you want to require unique entries, like having a unique user email

50
00:03:35,190 --> 00:03:39,420
so you don't have duplicate accounts, so you could add that on a face unique constraint.

51
00:03:39,780 --> 00:03:42,570
Again, that unique constraint is more of a sequel syntax.

52
00:03:42,840 --> 00:03:46,770
Instead, in Python, you would just say something like unique is equal to true, and we'll explore

53
00:03:46,770 --> 00:03:48,270
examples of this later on.

54
00:03:49,820 --> 00:03:55,640
So again, you end up choosing a field and then you can choose some sort of parameters or constraints

55
00:03:55,640 --> 00:03:57,200
that have something to do with that field.

56
00:03:57,620 --> 00:04:01,230
So we have the data type where the fields are chosen.

57
00:04:01,250 --> 00:04:04,610
Here we can see a character field, which is typically a string field.

58
00:04:04,970 --> 00:04:08,120
And then here we can see a max like a 30, which is just a constraint.

59
00:04:08,330 --> 00:04:10,640
This first thing can only have up to 30 characters.

60
00:04:10,860 --> 00:04:14,960
Again, it's up to you how long you want to make this sort of maxlength constraints.

61
00:04:15,290 --> 00:04:18,350
And we can check out the documentation for a list of all possible fields.

62
00:04:18,589 --> 00:04:22,790
And then within those fields, it gives you a list of all the possible arguments you can pass in.

63
00:04:24,400 --> 00:04:27,610
Jingle models can then also be later on connected through keys.

64
00:04:28,060 --> 00:04:32,800
So here we actually see two models a K two tables in the database being connected.

65
00:04:33,250 --> 00:04:36,220
The first model is a musician class model.

66
00:04:36,340 --> 00:04:40,210
And here we can see the musician has a first name, a last name and an instrument.

67
00:04:40,510 --> 00:04:41,860
They're all character fields.

68
00:04:42,130 --> 00:04:44,950
Now we can see they have a length constraint.

69
00:04:45,550 --> 00:04:48,880
Later on, we also have a class album model.

70
00:04:49,300 --> 00:04:50,860
And there we have an artist.

71
00:04:50,950 --> 00:04:54,820
But notice the artist has to be connected later on to a musician.

72
00:04:55,240 --> 00:04:59,320
So that is a direct relationship between the album and the musician.

73
00:04:59,740 --> 00:05:03,540
We can also see that the album has a name, a release date, a number of stars.

74
00:05:03,550 --> 00:05:08,920
So now we're seeing things like a character field, a deep field for dates and integer field for numbers

75
00:05:08,920 --> 00:05:10,600
that is integers and so on.

76
00:05:12,450 --> 00:05:16,770
So let's continue our discussion by exploring how to actually create a model and add fields to it.

77
00:05:17,250 --> 00:05:22,410
Later on in feature sections will actually be able to drastically advance our tango abilities by automatically

78
00:05:22,410 --> 00:05:27,090
creating templates simply by connecting them to a model that's known as class based views.

79
00:05:27,300 --> 00:05:31,050
And it's going to be super powerful tool once we understand how to use models.

80
00:05:31,410 --> 00:05:33,030
OK, I'll see you at the next lecture.

