H
H
hardwellZero2015-06-11 13:23:07
Django
hardwellZero, 2015-06-11 13:23:07

Customizing models/forms/views in django-registration?

Good afternoon.
I've asked quite a few questions here, but still haven't solved anything. (Yes, yes, I'm stupid)
In general, the point is that I need to add one more field when registering a user.
Using django-registration-redux
I do it like this now:
urls.py

from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
from store.forms import CustomRegistrationFormUniqueEmail
from registration.backends.default.views import RegistrationView

urlpatterns = patterns('',

    url(r'^$',
        TemplateView.as_view(template_name='index.html'),
        name='index'),

    url(r'^registration/$',
        RegistrationView.as_view(form_class=CustomRegistrationFormUniqueEmail),
        name='re-gistration_register'),

    url(r'^accounts/',
        include('registration.backends.simple.urls')),

    url(r'^accounts/profile/',
        TemplateView.as_view(template_name='profile.html'),
        name='profile'),

    url(r'^login/',
        'django.contrib.auth.views.login',
        name='login')
)

forms.py
from django import forms
from registration.forms import RegistrationFormUniqueEmail

class CustomRegistrationFormUniqueEmail(RegistrationFormUniqueEmail):
  new_field = forms.CharField(label='Your name', max_length=100)

models.py
from django.db import models
from django.contrib.auth.models import User as AbstractBaseUser, UserManager as BaseUserManager

# Create your models here.

class CustomUser(AbstractBaseUser):
    new_field = models.CharField(max_length=25)
    objects = BaseUserManager()

    USERNAME_FIELD = 'new_field'

What else do I need to put my new_field into the database? The user is entered successfully.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maxclax, 2015-06-18
@maxclax

models

class User(AbstractBaseUser):
    # менеджер модели по умолчанию
    objects = UserManager()

    class Meta:
        verbose_name = _('user.model.verbose_name')
        verbose_name_plural = _('user.model.verbose_name_plural')

    # переопределяем поле username
    USERNAME_FIELD = 'email'

    # емаил = логин
    email = models.EmailField(_('user.model.email'), max_length=100, unique=True)

    # имя
    firstname = models.CharField(_('user.model.firstname'), blank=True, max_length=50)

    # фамилия
    lastname = models.CharField(_('user.model.lastname'), blank=True, max_length=50)

    # блокировка пользователя
    is_disabled = models.BooleanField(_('user.model.is_disabled'), default=False)

    # доступ к админке
    is_admin = models.BooleanField(_('user.model.is_admin'), default=False)

    # супер пользователь
    is_superuser = models.BooleanField(_('user.model.is_superuser'), default=False)


    def get_full_name(self):
        return '%s %s' % (self.lastname, self.firstname)

    def get_short_name(self):
        return self.firstname

    def __str__(self):
        return str(self.email)

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

    @property
    def is_superuser(self):
        return self.is_admin

manager
from django.contrib.auth.models import BaseUserManager


class UserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(email=self.normalize_email(email), )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(email, password=password)
        user.is_admin = True
        user.save(using=self._db)
        return user

settings
# модель для персоны
AUTH_USER_MODEL = 'user.User'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question