2016-07-07 65 views
3

如何扩展django-oscar客户模型字段?我已经扩展了登记表包括多个字段,在apps/customer/forms.py如何扩展django oscar客户模型字段?

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

我也延长了[AbstractUser][1]的字段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(

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

当我尝试在外壳调试,我遇到了问题,即该模型是不可呼叫:

>>> from apps.customer.abstract_models import * 
>>> mg = UserManager() 
>>> mg.create_user('[email protected]', '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'小号文档中的说明会的工作,因为这是用于定制方法,而不是模型上的字段。

任何帮助,将不胜感激。

编辑:

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


AUTH_USER_MODEL = 'customer.User' 
+0

这是完全可能的字段添加到模型。你的问题是你的应用程序没有被加载。为了帮助我们确定问题,请发布:创建这个新代码的位置的详细信息(相对于项目根目录的文件/目录),INSTALLED_APPS和AUTH_USER_MODEL设置。 – solarissmoke

+0

我会扩展一个地址模型而不是用户模型。地址将具有is_default属性和外键给用户。 –

回答

4

有几个问题,我可以看到,解决这将有望解决您的问题:

  1. 移动你的AbstractUser子成apps/customer/models.py这就是Django就为模型。你已经把它放在apps/customer/abstract_models.py这是存储模型的非标准位置(奥斯卡只对抽象模型做这个 - 你不应该自己镜像这个位置)。 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.

+0

我已经执行了这些更改,但现在我遇到了auth |用户模型与我定制的客户用户模型冲突。之前已创建帐户以将AUTH_USER_MODEL属性更新为customer.User –

+0

这是更改用户模型时的固有风险 - 在现有数据库上执行并不容易或推荐使用。我在文档中添加了一些警告 - 除了这些之外无法帮助您。 – solarissmoke

+0

我可以在类中使用= get_user_model()来设置模型,然后在其上添加字段? –