2017-03-03 11 views
0

Djangoでカスタムユーザー登録フォームを作成しようとしています。これは、1)ユーザー名フィールドとして電子メールフィールドを使用します。2)余分なcompany_nameフィールドを追加してフォームに表示します。私は正常にユーザー名として電子メールを使用する最初の目標を達成することができますが、2番目のフィールドは、私は/アカウント/登録時にフォームに表示されていません。なぜDjangoのregistration-reduxカスタムフォームフィールドがフォームに表示されないのですか?

Models.py:

from django.db import models 
from django.contrib.auth.models import BaseUserManager 
from django.contrib.auth.models import AbstractBaseUser 
from django.contrib.auth.models import PermissionsMixin 
from django.utils.translation import ugettext_lazy as _ 
from django.conf import settings 
from phonenumber_field.modelfields import PhoneNumberField 
from django_countries.fields import CountryField 
from datetime import datetime  


class MyUserManager(BaseUserManager): 

     def _create_user(self, email, password, company_name, **extra_fields): 
      if not email: 
       raise ValueError('The Email must be set') 
      email = self.normalize_email(email) 
      user = self.model(email=email, company_name=company_name, **extra_fields) 
      user.set_password(password) 
      user.save() 
      return user 

     def create_superuser(self, email, password, company_name, **extra_fields): 
      extra_fields.setdefault('is_staff', True) 
      extra_fields.setdefault('is_superuser', True) 
      extra_fields.setdefault('is_active', True) 

      if extra_fields.get('is_staff') is not True: 
       raise ValueError('Superuser must have is_staff=True.') 
      if extra_fields.get('is_superuser') is not True: 
       raise ValueError('Superuser must have is_superuser=True.') 

      return self._create_user(email, password, company_name, **extra_fields) 

class User(AbstractBaseUser, PermissionsMixin): 

    email = models.EmailField(unique=True, null=True) 
    date_joined = models.DateTimeField(default=datetime.now) 
    company_name = models.CharField(max_length=300, null=True) 

    is_staff = models.BooleanField(
     _('staff status'), 
     default=False, 
     help_text=_('Designates whether the user can log into this 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.' 
     ), 
    ) 
    USERNAME_FIELD = 'email' 

    objects = MyUserManager() 

    def __str__(self): 
     return self.email 

    def get_full_name(self): 
     return self.email 

    def get_short_name(self): 
     return self.email 

Setting.py:私はthis registration formでCOMPANY_NAMEをもたらすことができますどのように

AUTH_USER_MODEL = 'usersignups.User' 

? models.pyで

答えて

0

from django.contrib.auth.models import User 
# Create your models here. 

class company(models.Model): 
    userkey = models.OneToOneField(User, primary_key=True) 
    company_name = models.CharField(max_length=50) 

forms.py

`from django.contrib.auth.models import User 
from registration.forms import RegistrationForm 
from .models import company 

class companyForm(RegistrationForm): 
    company_name = forms.CharField(label='label here', max_length=50) 
    class meta: 
     model = company 
     fields = ['company_name']` 

で会社の詳細の新モデルを定義し、compayFormをレンダリングするためにカスタムビューを作成し、それを願っています 保存しますあなたを助けます。私のためにそれはうまく動作します

関連する問題