如何扩展 django oscar 客户模型字段?

How do I extend the django oscar customer models fields?

如何扩展 django-oscar 客户模型字段?我已经扩展注册表以包含更多字段,在 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'))

我还在 apps/customer/abstract_models.py 中扩展了 [AbstractUser][1] 的字段。

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(

但是,当创建用户时,附加字段不会保存到数据库中。有没有更好的方法来扩展客户模型以包含我不知道的其他字段?

当我尝试在 shell 中进行调试时,我 运行 遇到了模型不可调用的问题:

>>> 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

我不确定 django oscar's 文档中给出的说明是否有效,因为那是针对自定义方法,而不是模型上的字段。

如有任何帮助,我们将不胜感激。

编辑:

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


AUTH_USER_MODEL = 'customer.User'

我发现了一些问题,解决这些问题有望解决您的问题:

  1. AbstractUser 的子 class 移动到 apps/customer/models.py 中,这是 Django 查找模型的地方。您已将它放在 apps/customer/abstract_models.py 中,这是用于存储模型的非标准位置(Oscar 仅对抽象模型执行此操作 - 您不应该自己镜像此位置)。 Django 不会在那里找到它们。

  2. 将您的 class 名称更改为 User 而不是 AbstractUser,因为您的最终模型不是抽象的。您还在 AUTH_USER_MODEL 中指定了 customer.User - 这两个需要匹配。

  3. 您在上面发布的模型 class 不完整,因此我们无法判断 - 但请确保它不包含 abstract = True Meta class.

  4. 运行 manage.py makemigrations 应该为您的新用户模型创建迁移(如果没有,那么您的应用程序结构仍然存在问题)。 (下一个运行manage.py migrate).

  5. 不要忘记在 models.py 底部导入其余(核心)客户模型:from oscar.apps.customer.models import *。没有这些,您将失去核心客户应用程序中的所有其他模型。

您还应注意 documentation 中有关更改用户模型(强调我的)的警告:

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.