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

标签 python django python-2.7 django-models django-oscar

如何扩展 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 的子类移动到 apps/customer/models.py 中,这是 Django 查找模型的地方。您已将其放在 apps/customer/abstract_models.py 中,这是存储模型的非标准位置(Oscar 仅对抽象模型执行此操作 - 您不应该自己镜像此位置)。 Django 不会在那里找到它们。

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

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

  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.

关于python - 如何扩展 django oscar 客户模型字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38247536/

相关文章:

python - 基于类的 View 扩展 UpdateView 未正确保存表单

python - 将 Django 与 SSL 结合使用以集成 PayPal

python - 如何使用 pandas 进行单元格合并

python - 将 xls 转换为 csv 并创建/更新 InMemoryUploadedFile Django

javascript - 有没有办法在 Django 中显示登录错误?

python - 搜索值为列表的 Python 字典

python - 使用 Python 单击下载按钮下载文件

python - pip freeze 在 Windows 安装中没有显示任何内容?

python - 控制 wsgiref simple_server 日志

python - 在 python 中计算 roc 曲线的二元分类器的 TPR 和 FPR