Sex = models.CharField(_("Пол"), max_length=3, choices=Sex.choices, default=Sex.M, null=True) ![]() User = models.OneToOneField(User, on_delete=models.CASCADE)īirthday = models.DateField(_("Дата рождения"), null=True,) Therefore, create a signals.py file inside the users app. Use Case:- using signals we can create a profile instance right when a new user instance is created inside the database.ĭjango recommends us to put signals in the app's directory as a single module. The connection between the senders and the receivers is done through “signal dispatchers”.Receiver: The receiver is usually a function that works on the data once it is notified of some action that has taken place for instance when a user instance is just about to be saved inside the database.Sender: is usually a model that notifies the receiver when an event occurs.In a signal, we have the following basic concepts. Signals are used to perform some action on modification/creation of a particular entry in database. It would be great if we can create the profiles automatically when a new user is created. Notice that we have to go to the admin page to create a profile whenever a user is created, but we don't wanna do that every now and then. The default profile picture that is given to users lives inside /media/ therefore, put any default image you want inside this directory with the name default.jpg. Till we create the frontend where uses will upload pictures, you can go to the admin panel and create images for registered users to see if all this is working well. We can now add files in the media root folder and django will serve them from the media url.Īs you can see from the image above, the profile picture that users upload will live inside /media/profile_images/your_image. One other important step is to register the profile model inside the users app admin.py.Ever worked with images in python before? if so, you probably know about pillow which is one of the most common image processing library that lets us do different kinds of image manipulations in python.ĭjango requires us to install this library whenever working with ImageField, so go to your terminal and type the following.įor the changes to take effect inside our database, let's run the migrations. ![]() ![]() So, whenever we print out the profile of a user, it will display his/her username.Īlright, there is a library we need in order for this to work. The dunder _str_ method converts an object into its string representation which makes it more descriptive and readable when an instance of the profile is printed out.The bio is just a text field where some information about users is stored.The second argument upload_to='profile_images' is the directory where images get uploaded to. The first argument of ImageField, default='default.jpg' is the default image to use for a user if they don't upload one themselves.The second argument on_delete=models.CASCADE means that if a user is deleted, delete his/her profile as well. The first argument of OneToOneField specifies which model the current model will be related to, which in our case is the User model.Enter fullscreen mode Exit fullscreen mode
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |