Django1.9:“函数"对象没有属性"_meta" [英] Django1.9: 'function' object has no attribute '_meta'

查看:95
本文介绍了Django1.9:“函数"对象没有属性"_meta"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django给出错误消息

Django Gives an error message

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')

由于类InceptionForm()发生错误.

an error occurs due to class InceptionForm().

错误:

AttributeError:函数"对象没有属性"_meta"

AttributeError: 'function' object has no attribute '_meta'

推荐答案

您已将User设置为 function get_user_model.您需要将其设置为调用该函数的结果:

You've set User equal to the function get_user_model. You need to set it to the result of calling that function:

User = get_user_model()

这篇关于Django1.9:“函数"对象没有属性"_meta"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆