darklow / django-suit

Modern theme for Django admin interface
http://djangosuit.com/
Other
2.32k stars 703 forks source link

Custom User Model And UserCreationForm #395

Open dmitry-saritasa opened 9 years ago

dmitry-saritasa commented 9 years ago

my models.py

from datetime import datetime, date
from django.db import models
from django.contrib.auth.models import User, AbstractUser, AbstractBaseUser
from django.contrib.auth.admin import UserAdmin
from django.utils import timezone
from django.contrib.postgres.fields import HStoreField
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from django.contrib import admin
from django_localflavor_us.models import USStateField
from django.core.validators import RegexValidator
from _commons.models.mixins import CreationModificationMixin

class AppUser(AbstractUser):
    _SEX = (
        ('M', 'Male'),
        ('F', 'Female'),
    )
    _pregex  = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    phone    = models.CharField(validators=[_pregex], max_length=16, blank=True)
    gender   = models.CharField(max_length=1, blank=True, null=True, choices=_SEX)
    birthday = models.DateField(blank=True, null=True)
    vericode = models.CharField(max_length=40, blank=True, null=True) # verification code over SMS?
    verified = models.DateTimeField(null=True, blank=True) # datetime stored when verification happened

    # for DJOSER
    # define required fields here, so you can call put /auth/me to update your profile
    REQUIRED_FIELDS = User.REQUIRED_FIELDS + ['first_name', 'last_name', 'gender', 'birthday']

    @property
    def age(self):
        if self.birthday:
            today = date.today()
            return today.year - self.birthday.year - ((today.month, today.day) < (self.birthday.month, self.birthday.day))
        return None

my admin.py

from django.contrib import admin
from django.utils.html import format_html, format_html_join
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.utils.html import urlize
from django import forms

from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from suit.widgets import SuitDateWidget, SuitTimeWidget, SuitSplitDateTimeWidget
from .models import AppUser

class AppUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = AppUser
        fields = ("username", "email", "password", "first_name", "last_name")

class AppUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        model = AppUser

class AppUserAdmin(UserAdmin):
    form = AppUserChangeForm
    add_form = AppUserCreationForm
    list_display = ('username', 'first_name', 'last_name', 'gender', 'phone', 'verified', 'age')
    readonly_fields = ('age', 'last_login', 'date_joined', )
    fieldsets = (
        ('Authorization', {
            'fields': ('username', 'password')
        }),
        ('Personal Info', {
            'fields': (('first_name', 'last_name'), 'email', 'gender', 'phone', 'birthday', 'verified',)
        }),
        ('Activity', {
            'fields': ('last_login', 'date_joined')
        }),
    )

admin.site.register(AppUser, AppUserAdmin)

this code does correctly display AppUserChangeForm, but it doesn't react at all on AppUserCreationForm - still got 3 fields, username, email, password only

SalahAdDin commented 9 years ago

Please, put a screen show for view your problem.

dmitry-saritasa commented 9 years ago

I'm using django 1.8.2

Edit form: http://pasteboard.co/1LAvS9xf.png

Add form: http://pasteboard.co/1LAzwAE7.png (a lot of fields missed)

my settings.py:


# Application definition
INSTALLED_APPS = (
    # Custom UI for Django Admin,
    'suit',
    # here we get our apps
    # it should be above so our custom user model is registered before
    # django CMS is registered
    'consents',

    # CMS
    # 'cms',  # django CMS itself
    # 'treebeard',  # utilities for implementing a tree
    # 'menus',  # helper for model independent hierarchical website navigation
    # 'sekizai',  # for javascript and css management
    # 'djangocms_admin_style',

    # General
    'django.contrib.sites',
    'django.contrib.auth',
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.postgres',
    'cacheops', # Caching DB requests on the fly
    'rest_framework', # Django Rest Framework
    'rest_framework.authtoken',
    'corsheaders', # Django CORS
    'djoser', # Django Auth API
    'djstripe', # Stripe integration
)

# Custom model for Auth
AUTH_USER_MODEL = 'consents.AppUser'
koddr commented 7 years ago

I have similar problem, but Django version is 1.11.1 Anybody help?

darklow commented 7 years ago

@koddr Does the problem goes away if you remove suit from INSTALLED_APPS? Can't see how can it be related to Django Suit.