django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -
i trying create customuser django project email username , add radio button truck , company. in registration process email-id registered per truck or company. mentioned radio button 'tag' , add manytomany field tag in emailuser model. when makemigrations, raising error : (fields.e300) field defines relation model 'abstractemailuser' either not installed, or abstract.
i quite new django , not sure whether have created correct code wants. please me solving this. here code,
models.py:
import django django.contrib.auth.models import ( abstractbaseuser, baseusermanager, permissionsmixin) django.core.mail import send_mail django.db import models django.utils import timezone django.utils.translation import ugettext_lazy _ class emailusermanager(baseusermanager): """custom manager emailuser.""" def _create_user(self, email, password, is_staff, is_superuser, **extra_fields): """create , save emailuser given email , password. :param str email: user email :param str password: user password :param bool is_staff: whether user staff or not :param bool is_superuser: whether user admin or not :return custom_user.models.emailuser user: user :raise valueerror: email not set """ = timezone.now() if not email: raise valueerror('the given email must set') email = self.normalize_email(email) is_active = extra_fields.pop("is_active", true) user = self.model(email=email, is_staff=is_staff, is_active=is_active, is_superuser=is_superuser, last_login=now, date_joined=now, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, email, password=none, **extra_fields): """create , save emailuser given email , password. :param str email: user email :param str password: user password :return custom_user.models.emailuser user: regular user """ is_staff = extra_fields.pop("is_staff", false) return self._create_user(email, password, is_staff, false, **extra_fields) def create_superuser(self, email, password, **extra_fields): """create , save emailuser given email , password. :param str email: user email :param str password: user password :return custom_user.models.emailuser user: admin user """ return self._create_user(email, password, true, true, **extra_fields) class abstractemailuser(abstractbaseuser, permissionsmixin): """abstract user same behaviour django's default user. abstractemailuser not have username field. uses email username_field authentication. use if need extend emailuser. inherits both abstractbaseuser , permissionmixin. following attributes inherited superclasses: * password * last_login * is_superuser """ email = models.emailfield(_('email address'), max_length=255, unique=true, db_index=true) is_staff = models.booleanfield( _('staff status'), default=false, help_text=_( 'designates whether user can log admin site.')) is_active = models.booleanfield(_('active'), default=true, help_text=_( 'designates whether user should treated ' 'active. unselect instead of deleting accounts.')) date_joined = models.datetimefield(_('date joined'), default=timezone.now) objects = emailusermanager() username_field = 'email' required_fields = [] class meta: verbose_name = _('user') verbose_name_plural = _('users') abstract = true # def __init__(self, *args, **kwargs): # super(abstractemailuser, self).__init__(*args, **kwargs) # if self.instance.pk: # self.fields['tag'].initial = self.instance.tag_set.all() def get_full_name(self): """return email.""" return self.email def get_short_name(self): """return email.""" return self.email def email_user(self, subject, message, from_email=none, **kwargs): """send email user.""" send_mail(subject, message, from_email, [self.email], **kwargs) # monkey patch django 1.7 avoid detecting migrations if django.version[:2] == (1, 7): last_login = abstractemailuser._meta.get_field('last_login') last_login.blank = true last_login.null = true last_login.default = models.fields.not_provided groups = abstractemailuser._meta.get_field('groups') groups.help_text = _('the groups user belongs to. user ' 'all permissions granted each of groups.') class emailuser(abstractemailuser): """ concrete class of abstractemailuser. use if don't need extend emailuser. """ choices = (('truck', 'truck'),('company', 'company'),) tag = models.manytomanyfield(abstractemailuser) class meta(abstractemailuser.meta): swappable = 'auth_user_model'
forms.py:
from django import forms django.contrib.auth import get_user_model django.contrib.auth.forms import readonlypasswordhashfield django.utils.translation import ugettext_lazy _ class emailusercreationform(forms.modelform): """a form creating new users. includes required fields, plus repeated password. """ error_messages = { 'duplicate_email': _("a user email exists."), 'password_mismatch': _("the 2 password fields didn't match."), } password1 = forms.charfield( label=_("password"), widget=forms.passwordinput) password2 = forms.charfield( label=_("password confirmation"), widget=forms.passwordinput, help_text=_("enter same password above, verification.")) choices= (('truck', 'truck'),('company', 'company'),) tag = forms.choicefield(choices=choices, label='tag', widget=forms.radioselect()) class meta: model = get_user_model() fields = ('email', 'tag',) def clean_email(self): """clean form email. :return str email: cleaned email :raise forms.validationerror: email duplicated """ # since emailuser.email unique, check redundant, # sets nicer error message orm. see #13147. email = self.cleaned_data["email"] try: get_user_model()._default_manager.get(email=email) except get_user_model().doesnotexist: return email raise forms.validationerror( self.error_messages['duplicate_email'], code='duplicate_email', ) def clean_password2(self): """check 2 password entries match. :return str password2: cleaned password2 :raise forms.validationerror: password2 != password1 """ password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 , password2 , password1 != password2: raise forms.validationerror( self.error_messages['password_mismatch'], code='password_mismatch', ) return password2 def save(self, commit=true): """save user. save provided password in hashed format. :return custom_user.models.emailuser: user """ user = super(emailusercreationform, self).save(commit=false) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user class emailuserchangeform(forms.modelform): """a form updating users. includes fields on user, replaces password field admin's password hash display field. """ password = readonlypasswordhashfield(label=_("password"), help_text=_( "raw passwords not stored, there no way see " "this user's password, can change password " "using <a href=\"password/\">this form</a>.")) class meta: model = get_user_model() exclude = () def __init__(self, *args, **kwargs): """init form.""" super(emailuserchangeform, self).__init__(*args, **kwargs) f = self.fields.get('user_permissions', none) if f not none: f.queryset = f.queryset.select_related('content_type') def clean_password(self): """clean password. regardless of user provides, return initial value. done here, rather on field, because field not have access initial value. :return str password: """ return self.initial["password"]
admin.py:
from django.contrib import admin django.contrib.auth.admin import useradmin django.utils.translation import ugettext_lazy _ .forms import emailuserchangeform, emailusercreationform .models import emailuser class emailuseradmin(useradmin): """emailuser admin model.""" fieldsets = ( (none, {'fields': ('email', 'password', 'tag')}), (_('permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}), (_('important dates'), {'fields': ('last_login', 'date_joined')}), ) add_fieldsets = (( none, { 'classes': ('wide',), 'fields': ('email', 'password1', 'password2', 'tag') } ), ) # forms add , change user instances form = emailuserchangeform add_form = emailusercreationform # fields used in displaying user model. # these override definitions on base useradmin # reference specific fields on auth.user. list_display = ('email', 'is_staff', ) list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups', 'tag') search_fields = ('email',) ordering = ('email',) filter_horizontal = ('groups', 'user_permissions', 'tag',) # register new emailuseradmin admin.site.register(emailuser, emailuseradmin)
Comments
Post a Comment