1

I want to add Mobile filed to be saved to auth_user table when i register.

models.py

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

class UserRegModel(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    mobile = models.CharField(max_length=100)

forms.py

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

class UserRegisterForm(UserCreationForm):
    mobile = forms.CharField(max_length=15)

    class Meta:
        model = User
        fields = ['username','email','mobile','password1','password2']
Sumit Nayak
  • 307
  • 3
  • 13

2 Answers2

1

If you want to store all user data together, you should substitute a user model instead of creating a OneToOne relationship. Judging by the current code you will get 2 tables - one for standard Django user and one connected to it with mobile data.

Here you can read more about substituting a user and the difference between these 2 approaches:

Extending the User model with custom fields in Django

Or directly in the documentation:

https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model

Igor Belkov
  • 446
  • 3
  • 8
  • I dont want to add extra details [mobile] in profiles. I want to store in same table auth_user. – Sumit Nayak Jun 07 '19 at 10:54
  • @SumitNayak and so it will be - if you will add AUTH_USER_MODEL to settings with the path to custom user class, which extends either AbstractBaseUser (more customizable version) or AbstractUser (more or less old User class you can extend) it will add your additional fields from custom class to the standard user class and store it in one auth_user table. Tho, I strongly recommend to check out the first link, because this approach has its own nuances. – Igor Belkov Jun 07 '19 at 11:18
  • Sure, Let me check – Sumit Nayak Jun 07 '19 at 11:22
  • i was successful doing so, but now when im creating Registration form im not able to do so, from django.contrib.auth.forms import UserCreationForm – Sumit Nayak Jun 10 '19 at 11:12
  • Hey @IgorBelkov Thanks for the help, I was able to do the task. – Sumit Nayak Jun 11 '19 at 06:50
  • @Sumit Nayak I glad, that it helped! Good luck :) – Igor Belkov Jun 11 '19 at 11:08
0

Click here for the first image
Click here for the second image

Do this and type these codes in terminal

python manage.py makemigrations
python manage.py migrate
MD Mushfirat Mohaimin
  • 1,966
  • 3
  • 10
  • 22