How to Create a Profile to an user

Question:

im relative new to django, before i only created simple applications that did not require any user stuff, now however i need to create a website with users, for now i created user register, log in and log out, but im struggling to create the profile for the user, should i create an app only for profile ? or maybe put the profile together on the models inside the user app where is the user authentication is, how do i go about the views on this matter ? if someone has a simple example that even a dumb person like me can understand i would appreciate a lot, thanks in advance.

For now i tried to create an App for profile and make a model for profile with the OnetoOne Field:

user = models.OneToOneField(settings.AUTH_USER_MODEL, null = True, on_delete= models.CASCADE)

However im having a trouble where the profile does not start with the user, meaning that the user will have to create the profile and then when he wants to change he needs to go on the other option called edit, which is a bit awkward

Asked By: emanuel.thiago001

||

Answers:

So the older way of implementing this was to create a One-To-One. Similar to what you‘ve pointed out, except that null should be in this case False.

This is a valid and often used approach, but it became more likely to modify the User model directly. Therefore you can add new fields to the existing ones and overwrite existing ones (f.ex. add custom validation logic) or f.ex. a complete new phone_number field.

Its basically this:

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    pass

Anyways you can also stick to the Profile one-to-one. It requires a second model and you need to handle the object creation on the first login. Django has a signal which can be called automatically when a user logs in (user_logged_in).

I suggest you to got with the AbstractUser. Its easier to integrate.

Answered By: mika