Home > OS >  How to add columns to auth_user in django postgresql?
How to add columns to auth_user in django postgresql?

Time:12-19

Hello i want to register user in django. In django there is just fileds for basic datas email and username, but i want to add more like; about, user_profile_picture_url, address etc.

It creates the model perfectly but... It's not saving it to database

also makemigrations and migrate does not work!

enter image description here

there is just username, first_name, last_name and email

views.py

from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.contrib import messages
from profiles.forms import Register_User_Form
from profiles.models import User_Profile


def register_user(request):
    if request.method == 'POST':
        form = Register_User_Form(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data["username"]
            password = form.cleaned_data["password1"]
            email = form.cleaned_data["email"]

            about = form.cleaned_data["about"]
            phone_num = form.cleaned_data["phone_num"]
            picture_url = form.cleaned_data["picture_url"]

            User_Profile(name=username, email=email, about=about,
                         phone_num=phone_num, picture_url=picture_url)
            print("Model Created")
            user = authenticate(username=username, password=password)
            login(request, user)
            messages.success(request, "Registration Successfull")
            return redirect('/')
    else:
        form = Register_User_Form()

    return render(request, 'registration/register.html', {"form": form})

models.py

from django.db.models import *


class User_Profile(Model):
    picture_url = CharField(max_length=120, default="")
    name = CharField(max_length=30, default="")
    about = CharField(max_length=250, default="")
    email = CharField(max_length=50, default="")
    phone_num = CharField(max_length=15, default="")

forms.py

from django.forms import *
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User


class Register_User_Form(UserCreationForm):
    username = CharField(label="Username", max_length=36)
    email = CharField(label="Email", max_length=120, widget=EmailInput())
    password1 = CharField(label="Password", max_length=36, widget=PasswordInput())
    password2 = CharField(label="Confirm Password", max_length=36, widget=PasswordInput())

    phone_num = CharField(label="Phone Number (optional)", max_length=15, widget=NumberInput())
    about = CharField(label="About you! (optional)", max_length=300, required=False)
    picture_url = CharField(label="Profile Photo URL (optional)", max_length=200, required=False)

    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2",
                  "phone_num", "about", "picture_url")

    def save(self, commit=True):
        user = super(Register_User_Form, self).save(commit=False)
        user.username = self.cleaned_data["username"]
        user.email = self.cleaned_data["email"]
        user.password1 = self.cleaned_data["password1"]
        user.password2 = self.cleaned_data["password2"]
        user.phone_num = self.cleaned_data["phone_num"]
        user.about = self.cleaned_data["about"]
        user.picture_url = self.cleaned_data["picture_url"]

        if commit:
            user.save()
        return user

CodePudding user response:

Django provides built-in support for extending the user with your own properties: User Profiles.

  • Related