【发布时间】:2016-11-09 21:27:21
【问题描述】:
如何扩展 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'
【问题讨论】:
-
完全可以在模型中添加字段。您的问题是您的应用程序没有被加载。为了帮助我们确定问题,请发布:您在何处创建此新代码的详细信息(与您的项目根目录相关的文件/目录)、
INSTALLED_APPS和AUTH_USER_MODEL设置。 -
我会扩展地址模型而不是用户模型。地址将具有 is_default 属性和用户的外键。
标签: python django python-2.7 django-models django-oscar