I want to extend the User model. I followed the steps mentioned in this doc. I made a new app extended_user
whose models.py reads as :
from django.db import models
from oscar.apps.customer.abstract_models import AbstractUser
from django.utils.translation import ugettext_lazy as _
class User(AbstractUser):
nickname = models.CharField(_("nick_name"), max_length=50, null=True, blank=True)
def get_full_name(self):
full_name = '%s %s' % (self.last_name.upper(), self.first_name)
return full_name.strip()
In settings.py I mention
AUTH_USER_MODEL = "extended_user.User"
I make and run migrations. In profile view I can see nickname
field but in Profile Edit view I don't. What do I have to do to see the newly added field in the Profile Edit form ?
I assume that you do not use a separate rofile class. In that case Oscar sets ProfileForm
to point to the UserForm
class.
That class in turn has a more-or-less hardcoded list of fields
. (In reality it says "whichever fields exist out of this list".)
The easiest way to go forward from here is to override customer.forms.ProfileForm
with your own class that uses your newly defined User
model and the list of fields that better suits your usecase. (Create a your_app.customer.forms
module and define a ProfileForm
inside.)
python3 manage.py oscar_fork_app customer
- add your app on your settings :
from oscar import get_core_apps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'django.contrib.flatpages',
'widget_tweaks',
'YOUR_APP',
] + get_core_apps(['customer'])
- The last part is to put this content of
customer/forms.py
:
- it changes the Model with yours to access new added fields;
- extends existing UserForm to keep all clean data;
- change
ProfileForm
var as Oscar use it for render form of profile to point to your new form.
from oscar.apps.customer.forms import UserForm as CoreUserForm
from user.models import User
from django import forms
from oscar.core.compat import existing_user_fields
class UserForm(CoreUserForm):
class Meta:
model = User
fields = existing_user_fields(['username', 'first_name', 'last_name', 'email'])
ProfileForm = UserForm