Django1.9: 'function' object has no attribute '_meta'

…衆ロ難τιáo~ 提交于 2019-12-11 06:08:30

问题


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

an error occurs due to class InceptionForm().

Error:

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


回答1:


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


来源:https://stackoverflow.com/questions/38290644/django1-9-function-object-has-no-attribute-meta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!