[英]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.