Django1.9: 'function' 对象没有属性 '_meta'
Django1.9: 'function' object has no attribute '_meta'
Django 给出错误信息
forms.py:
from django import forms
from django.contrib.auth import authenticate, get_user_model, login, logout
from django.contrib.auth.forms import UserCreationForm
User = get_user_model
class UserLoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
def clean(self, *args, **kwargs):
username = self.cleaned_data.get("username")
password = self.cleaned_data.get("password")
user = authenticate(username=username, password=password)
#user_qs = User.objects.filter(username=username)
#if user_qs.count() == 1:
# user = user_qs.first()
if username and password:
user = authenticate(username=username, password=password)
if not user:
raise forms.ValidationError("This user does not exist.")
if not user.check_password(password):
raise forms.ValidationError("Incorrect password.")
if not user.is_active:
raise forms.ValidationError("User is not active.")
return super(UserLoginForm, self).clean(*args, **kwargs)
class InceptionForm(forms.ModelForm):
email2 = forms.EmailField(label='Confirm Email')
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
由于 class InceptionForm() 而发生错误。
错误:
AttributeError: 'function' 对象没有属性 '_meta'
您已将用户设置为等于 函数 get_user_model
。您需要将其设置为调用该函数的结果:
User = get_user_model()
这个库生成用户模型表单:
from django.cotrib.auth import get_user_model
将授权用户连接到 user_model_form:
User = get_user_model()
Django 给出错误信息
forms.py:
from django import forms
from django.contrib.auth import authenticate, get_user_model, login, logout
from django.contrib.auth.forms import UserCreationForm
User = get_user_model
class UserLoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)
def clean(self, *args, **kwargs):
username = self.cleaned_data.get("username")
password = self.cleaned_data.get("password")
user = authenticate(username=username, password=password)
#user_qs = User.objects.filter(username=username)
#if user_qs.count() == 1:
# user = user_qs.first()
if username and password:
user = authenticate(username=username, password=password)
if not user:
raise forms.ValidationError("This user does not exist.")
if not user.check_password(password):
raise forms.ValidationError("Incorrect password.")
if not user.is_active:
raise forms.ValidationError("User is not active.")
return super(UserLoginForm, self).clean(*args, **kwargs)
class InceptionForm(forms.ModelForm):
email2 = forms.EmailField(label='Confirm Email')
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
由于 class InceptionForm() 而发生错误。
错误:
AttributeError: 'function' 对象没有属性 '_meta'
您已将用户设置为等于 函数 get_user_model
。您需要将其设置为调用该函数的结果:
User = get_user_model()
这个库生成用户模型表单:
from django.cotrib.auth import get_user_model
将授权用户连接到 user_model_form:
User = get_user_model()