Home > Blockchain >  Django - How to add extra fields to User model?
Django - How to add extra fields to User model?

Time:10-14

I need to add new fields to the " real Django User model ". Why? Because I have two forms in my template with post methods in two different pages.

And I created two models, each one saves the corresponding form data in the database.

In my views I had to write two different functions for the forms pages.

The 1st one: when there is a post request, it saves the data in the 1st Model.

The 2nd one: Does the same as 1st but it saves the data in the 2nd Model.

I tried to create another new model3 which contains model1 model2 fields (Didn't delete the previous models)! But when I submit the 1st form it saves data in an object, and when I submit the 2nd form data in another new object of model3 !

So I canceled this method and now I have to try another way to do the task, So I saved the 1st form data ( which are a login info ( email, pass ) and when a user submits the form, it creates a new user with

user = User.objects.create_user(request.POST.get("email"),request.POST.get("email"),password)

That worked fine, and now I can modify the credentials in the views with just user.username = newemail and user.password with newpassword.. but I couldn't do that with the 2nd form because the User model hasn't as much fields as the Model2! And also when I try it with

data = Model3(fielname = request.POST.get("addressLineForExample")
data.save()

it causes an error saying that I didn't specify for which user id I am going to save it!

I've seen videos and docs about the OneToOneField method! Then I figured out it just shows the model3 fields in the user page in 127.0.0.1:8000/admin, and not adding real fields to the User Model!

I came with the idea of changing user.newaddedfield = any Model3 field! I just want to realize it that way!

CodePudding user response:

You can extend Django's AbstractUser to create your own custom user model on top of Django's User model. For example:

File: project/user/models.py

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    custom_field = models.BooleanField(default=False)

Then you need to run

python manage.py makemigrations
python manage.py migrate

in your terminal for creating related model in the database.

In order to see this model in your admin page, register the model using the following snippet:

File: project/user/admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User

admin.site.register(User, UserAdmin)
  • P.S. I strongly recommend adding AUTH_USER_MODEL = "user.User" to project/project/settings.py in order to be able to fully use Django's default User functionalities (including Django Rest Framework's authentication ingtegrations).

  • P.S. Make sure you have the app's name listed under INSTALLED_APPS in your settings.py

CodePudding user response:

Solution:

I went back with creating another new model3 which contains model1 model2 fields

Then, when I submit the first form, I do:

data = Model3(field1 = request.POST.get("ValueOfThe1stFormField1")
data.save()

Now the Model3 modem will be filled with the 1st form's data but not yet with the 2nd's.

So in the 2nd function where a user should submit data in the 2nd form, I do an update to the related model3 object like:

Model3.objects.filter(field1=field1OfThe1stForm).update(field2=anything, field3=anything,...)

Hope it helps anyone who is trying to merge two (or more) models in one other model that should contain all of the previous model fields.

  • Related