Django: Getting Started Transcripts
Chapter: Users and Account Management
Lecture: Add profile to user object

Login or purchase this course to watch this video and the rest of the course contents.
0:03 I'm going to create the user profile model object in the people app. Let me just open up people models.py. And I'll start by replacing the boilerplate.
0:22 Alright, that's all the imports I'll need I'll be referencing the user model so I need that. The profile is just a model object.
0:29 So the usual models.model thing is required and then the next to imports are for registering the signal.
0:37 Our user profile is going to track the favorite books of the user, so in addition to having a relationship with the user object,
0:43 I'm also going to create a relationship with some books. Hence importing catalog.models book.
1:03 The reader class is going to be my user profile. The 1-1 field forms the 1-1 relationship with a user object.
1:12 Similar to foreign keys you've seen before, this requires an on delete flag to decide what to do if the user gets deleted.
1:19 Not much point in having a profile without the user. So I've set this to cascade, meaning the profile will get deleted automatically if the user is.
1:27 A many to many field is a relationship that indicates this object is related to many other objects of a type.
1:35 This allows the reader to have more than one favourite book. This field is used like a query manager.
1:41 You don't use it directly. You call methods on it to get their relations. For example, .all will return all of the users favorite books.


Talk Python's Mastodon Michael Kennedy's Mastodon