2016-07-07 43 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()來設置模型,然後在其上添加字段? –