简体   繁体   English

AttributeError: 'User_Profile' 对象没有属性 '__name__'

[英]AttributeError: 'User_Profile' object has no attribute '__name__'

My goal is to get my forms.py to send the email form content to a different model into my Django database, from the default User model in Django to my custom-made User_Profile model.我的目标是让我的forms.py将电子邮件表单内容发送到我的 Django 数据库中的不同模型,从 Django 中的默认用户模型到我定制的 User_Profile 模型。 I believe if I can fix the error above that my goal will be accomplished.我相信如果我能解决上面的错误,我的目标就会实现。

In my forms.py file, I am importing a class from the model.py then using the function again in the forms.py , which is what is causing the error.在我forms.py文件,我从导入类model.py然后在再次使用功能forms.py ,这是什么原因造成的错误。 I did my research on __ name__ with classes, and from what I understood, it has to do with using the same class two different times and it doesn't know which one to run or something.我用类对__ name__进行了研究, __ name____ name__ ,它与两次不同时间使用同一个类有关,它不知道要运行哪个类或其他什么。 If someone could explain __name __ with classes better and help me fix the error, I would much much appreciate that.如果有人能用类更好地解释__name __并帮助我修复错误,我将不胜感激。

Here is my models.py :这是我的models.py

class User_Profile(models.Model):
    user = models.OneToOneField(User, null = True, blank = True, on_delete = models.CASCADE)
    name = models.CharField(max_length = 200, null = True)
    phone = models.CharField(max_length = 200, null = True)
    email = models.CharField(max_length = 200, null = True)
    profile_image = models.ImageField(null = True, blank = True)
    data_created = models.DateTimeField(auto_now_add = True, null = True)

    def __str__(self):
        return self.name

Here is my forms.py in another directory:这是我在另一个目录中的forms.py

class RegisterForm(UserCreationForm):
    email = forms.EmailField()

    class Meta():
        model = User_Profile()
        fields = ["username", "email", "password1", "password2"]
        
        def __str__(self):
            return self.name

The issue is not really about __name__ , it's just the symptom about the real problem.问题不是真正关于__name__ ,它只是真正问题的症状。 The problem is because of passing an instance in the Meta class of RegisterForm instead of the class.问题是因为在RegisterForm的 Meta 类而不是类中传递了一个实例。 So to fix:所以要修复:

    class Meta():
        model = User_Profile # <-- Remove the parens
        fields = ["username", "email", "password1", "password2"]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError: 'SparseCategoricalCrossentropy' object 没有属性 '__name__' - AttributeError: 'SparseCategoricalCrossentropy' object has no attribute '__name__' AttributeError:&#39;str&#39;对象没有属性&#39;__name__&#39;返回 - AttributeError: 'str' object has no attribute '__name__' Returned 动态更改函数__name__会引发AttributeError:&#39;method&#39;对象没有属性&#39;__name__&#39; - Dynamically changing a function __name__ throws AttributeError: 'method' object has no attribute '__name__' 类装饰器对象没有属性__name__ - Class decorator object has no attribute __name__ 'Role'对象没有属性'__name__' - 'Role' object has no attribute '__name__' AttributeError: &#39;CustomAugment&#39; 对象在保存 TensorFlow 模型时没有属性 &#39;__name__&#39; - AttributeError: 'CustomAugment' object has no attribute '__name__' when saving TensorFlow model AttributeError :(类)对象没有属性&#39;__name__&#39;创建ModelForms [Django&Python2.7] - AttributeError: (Class) object has no attribute '__name__' Creating ModelForms [Django & Python2.7] /profiles/user-profile/2/ 'int' 处的 AttributeError object 没有属性 '_meta' - AttributeError at /profiles/user-profile/2/ 'int' object has no attribute '_meta' /profile/ &#39;function&#39; 对象的 AttributeError 没有属性 &#39;object - AttributeError at /profile/ 'function' object has no attribute 'object Django AttributeError at /accounts/profile/ 'User' object has no attribute 'get' while update profile - Django AttributeError at /accounts/profile/ 'User' object has no attribute 'get' while update profile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM