#100DaysOfWeb in Python Transcripts
Chapter: Days 53-56: Django part 2 - registration and login
Lecture: Update the Quote model to associate quotes with users

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Great, now that we have login in place
0:02 we can start working on adding the user to the quotes model
0:06 so that we have quotes associated with the user
0:09 and then we can set up that only the user that is logged in
0:12 can edit his or her own quotes.
0:17 So let's cd into my quotes app.
0:20 Open models and import the User model
0:24 which is provided by Django out of the box.
0:30 Add this django.contrib.auth.models.
0:38 And then I need to make a ForeignKey relation
0:41 to that user model
0:43 and the way to do that is to say
0:49 models.ForeignKey
0:53 specify the model that we link to, so user.
0:57 And on delete we can do two things
0:59 so if the user gets deleted
1:02 do we want to cascade the deletes
1:03 so when the user gets deleted, delete all the quotes
1:06 or do we want to put a no value into the quotes.
1:10 On this case I'm going to choose the former
1:13 and that's specified as models cascade
1:20 and I'm going to allow default
1:23 of blank is true and no is true
1:28 'cause I already used some quotes in the database
1:30 which are not having the user associated.
1:32 This makes it easier to have a migration
1:35 without asking us questions or asking us for defaults.
1:39 Again the models cascade
1:41 is to make sure that if the user gets deleted
1:44 all the linked quotes get deleted as well.
1:47 Then I'm updating the str method
1:50 to also put information in here
1:53 who submitted the quote. And that's it.
2:04 Now we need to make a migration because our model changed.
2:08 So I'm going to the outer terminal window
2:11 and I'm going to type python manage.py makemigrations
2:19 And that made this migration.
2:24 And we see here that the field was added to the quote model.
2:28 And it's a ForeignKey.
2:33 Then we can migrate to actually commit it to the database.
2:38 And that relation now exists. Can even check that.
2:54 And here under quote's quote we have a new field
2:58 user id of integer. Let's run the server.
3:08 And nothing will happen yet.
3:12 Other than that, this is still working
3:14 and in the next video
3:16 we're going to update the views and templates
3:19 to accommodate this change.
3:22 Users will be associated with quotes
3:24 and we'll make sure that users
3:26 can only edit their own quotes.