简体   繁体   中英

How do I extend the django oscar customer models fields?

How do I extend the django-oscar customer models fields? I have extended the registration form to include more fields, in apps/customer/forms.py

class EmailUserCreationForm(forms.ModelForm):
    email = forms.EmailField(label=_('Email address'))
    password1 = forms.CharField(
        label=_('Password'), widget=forms.PasswordInput,
        validators=password_validators)
    password2 = forms.CharField(
        label=_('Confirm password'), widget=forms.PasswordInput)

    #### The extra fields I want to add #####

    first_name = forms.CharField(label=_('First name'))
    last_name = forms.CharField(label=_('Last name'))
    business_name = forms.CharField(label=_('Business name'))
    business_address = forms.CharField(label=_('Business address'))
    city = forms.CharField(label=_('City'))

I have also extended the [AbstractUser][1] 's fields in apps/customer/abstract_models.py .

class AbstractUser(auth_models.AbstractBaseUser,
                   auth_models.PermissionsMixin):
    """
    An abstract base user suitable for use in Oscar projects.

    This is basically a copy of the core AbstractUser model but without a
    username field
    """
    email = models.EmailField(_('email address'), unique=True)
    first_name = models.CharField(
        _('First name'), max_length=255, blank=True)
    last_name = models.CharField(
        _('Last name'), max_length=255, blank=True)
    is_staff = models.BooleanField(
        _('Staff status'), default=False,
        help_text=_('Designates whether the user can log into this admin '
                    'site.'))
    is_active = models.BooleanField(
        _('Active'), default=True,
        help_text=_('Designates whether this user should be treated as '
                    'active. Unselect this instead of deleting accounts.'))
    date_joined = models.DateTimeField(_('date joined'),
                                       default=timezone.now)
    #######################################
    # Additional user fields I have added #
    #######################################
    business_name = models.CharField(
        _('Business name'), max_length=255, blank=True)
    business_address = models.CharField(
        _('Business address'), max_length=255, blank=True)
    city = models.CharField(

However, when a user is created, the additional fields are not getting saved to the database. Is there a better way of extending the customer model to include additional fields I'm not aware of?

When I try to debug in the shell, I run into the issue where the model is not callable:

>>> from apps.customer.abstract_models import *
>>> mg = UserManager()
>>> mg.create_user('testemail@test.com', 'testpassword', buisness_name='test_business')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "<my_working_dir>/apps/customer/abstract_models.py", line 34, in create_user
    last_login=now, date_joined=now, **extra_fields)
TypeError: 'NoneType' object is not callable

I'm not sure the instructions given in django oscar' s docs will work, as that is for customizing methods, not fields on the model.

Any help would be appreciated.

Edit:

INSTALLED_APPS = INSTALLED_APPS + get_core_apps(
    ['apps.shipping',
     'apps.checkout',
     'apps.partner',
     'apps.catalogue',
     'apps.customer',
     ])


AUTH_USER_MODEL = 'customer.User'

There are a few issues that I can see, resolving which will hopefully fix your problem:

  1. Move your subclass of AbstractUser into apps/customer/models.py which is where Django looks for models. You've put it in apps/customer/abstract_models.py which is a non-standard location for storing models (Oscar does this for abstract models only - you are not supposed to mirror this location yourself). Django will not find them there.

  2. Change your class name to User instead of AbstractUser , because your final model is not abstract. You are also specifying customer.User in your AUTH_USER_MODEL - these two need to match.

  3. The model class you have posted above is incomplete so we cannot tell - but make sure that it doesn't contain abstract = True in the Meta class.

  4. Run manage.py makemigrations which should create migrations for your new user model (if it doesn't then there's still something wrong with your app structure). (Next run manage.py migrate ).

  5. Don't forget to import the rest of the (core) customer models at the bottom of your models.py : from oscar.apps.customer.models import * . Without these you will lose all the other models that live in the core customer app.

You should also take note of the warning in the documentation regarding changing the user model (emphasis mine):

Changing AUTH_USER_MODEL has a big effect on your database structure. It changes the tables that are available, and it will affect the construction of foreign keys and many-to-many relationships. If you intend to set AUTH_USER_MODEL, you should set it before creating any migrations or running manage.py migrate for the first time.

Changing this setting after you have tables created is not supported by makemigrations and will result in you having to manually fix your schema, port your data from the old user table, and possibly manually reapply some migrations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM