1
votes

Below is what i have tried to add a phone number column in user model:-

from django.contrib.auth.models import AbstractUser

# models.py

# Import the basic Django ORM models library
from django.db import models

from django.utils.translation import ugettext_lazy as _


# Subclass AbstractUser
class User(AbstractUser):
    phonenumber = models.CharField(max_length=15)

    def __unicode__(self):
        return self.username

# forms.py

from django import forms

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

class UserForm(forms.Form):

    class Meta:
        # Set this form to use the User model.
        model = get_user_model

        # Constrain the UserForm to just these fields.
        fields = ("first_name", "last_name", "password1", "password2", "phonenumber")

    def save(self, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.password1 = self.cleaned_data['password1']
        user.password2 = self.cleaned_data['password2']
        user.phonenumber = self.cleaned_data['phonenumber']
        user.save()

# settings.py

AUTH_USER_MODEL = "users.User"
ACCOUNT_SIGNUP_FORM_CLASS = 'users.forms.UserForm'

But on this change, it raises the OperationalError: (1054, "Unknown column 'users_user.phonenumber' in 'field list'")

I already uses syncdb and migrate options but nothing will work, As i am very new to django, please help me

I am using:- Python2.7, Django 1.6, django-allauth 0.15.0

2
Why not use a regular ModelForm? Meta doesn't do anything in a regular form. Anyway, the full traceback will help here.knbk
Can you please provide me any nice documentation for adding these, i don't have full concept of all that, i still have to learn those things, please provide me the documentationSheesh Mohsin
Modelforms. The full traceback is the error including the 'stack' of functions called before the error was encountered. You should see it somewhere on the error page.knbk

2 Answers

0
votes

Actually Problem is occuring that the field or column i created does not actually created in database and running syncdb doesn't work in this case, then finally i got the answer, we have to use South to create the schema migration to create your new table.

python manage.py schemamigration appname --auto

Once we have this migration written and tested to our liking, you can run the migration and verify that it did what we expected via the Django admin.

python manage.py migrate

And also made some changes in forms.py

# forms.py

class UserForm(ModelForm):

    class Meta:
        # Set this form to use the User model.
        model = User

        # Constrain the UserForm to just these fields.
        fields = ("username", "email", "phonenumber")

    def save(self, user):
        user.username = self.cleaned_data['username']
        user.email = self.cleaned_data['email']
        user.phonenumber = self.cleaned_data['phonenumber']
        user.save()
-1
votes

Try something like this:

# models.py

# Subclass AbstractUser
class CustomUser(AbstractUser):
    phonenumber = models.CharField(max_length=15)

    def __unicode__(self):
        return self.username

# settings.py

AUTH_USER_MODEL = 'myapp.CustomUser'

The idea is that you want to be pointing to and using your subclass, rather than the original user class. I think you'd need to make these changes in your form code as well, but just test that first (and run manage.py syncdb) to see if your new class appears with the phone number and all the other user fields.