Friendship Cycle

Ini adalah kisahku tentang pertemanan. Bukan hanya pertemanan satu dua orang atau kelompok saja, tapi mencakup seluruh kehidupan pertemananku. Kisah pertemananku selalu sama, aku sebut itu sebagai…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Django User Profiles

Any web app must allow users to build a profile, you dont want thsi data clogging up your user model, so we split this off into its own model, to do so lets look at ways we could achieve this.

We could override the save method of our user model to create a user profile and populate some data from our user model in the profile, an example would be as such:

We can see an obvious flaw with this, were overriding the save method, so every time the user model gets saved it will run the code. We could put some logic in to check if a user has a profile, but this is putting too much logic into the save method, and becomes a pain to test and maintain.

Enter signals, a powerful way to handles these types of events.

Django signals is an excellent built in layer to Django. The documentation descives signals as:

If you have not used it i would recommend it hands down as the best way to perform such actions, as built in with signals are some receiver functions to catch these these actions.

We will start by creating a new file called signals.py, you can store your signals anywhere, however storing your code in a organised manner helps you the developer! Inside of signals.py were going to start by importing the following:

We are importing the the decoractor of reciever, and the signal we want to use, you can get a full breakdown of what each import does from the Django docs.

We are then going to build our signal receiver function, by coding out the following:

So we have just wrote our a simple function that is called whenever the user model is saved, but will only fire if the user model is created, so only new users.

By default it wont work we need to enable it and tell Django where the recievers are. There are 2 ways we can turn it on, i prefer the following way what we need to do is tell our app to use a custom config and point that config to where the receiver functions are.

This tells the app to use our custom config over the standard default config. We now need to make our config class, open up apps.py and update your current class with the following

At this point you can test, runserver and add a user and check the profile has been added.

Bonus note we probably want to update our Django admin with our user profile inline rather than a separate model or app. You can do this by updating your admin.py to the following:

This will append their profile at the bottom of the Django Admin page for the current user.

Add a comment

Related posts:

Splendia se deshace de su inventario de hotel

Recientemente Splendia y su filial de viajes privados Voyage privé le ha enviado un email a parte de su inventario de Hotel, este ha sido su mensaje: Así que a partir del día 20 de Diciembre…

The Tiger and the Strawberry

The following ancient story- known as a “Koan”- from the Zen Buddhist faith tradition offers a useful insight into 2020. Once upon a time, as a man was walking through a forest, he saw a tiger…

How to Prevent the Antivirus From Slowing Down Your Computer?

Some people are reluctant to install an antivirus for a very simple reason: it slows down the computer . Especially when the computer is turned on, it usually takes much longer to start up; but also…