简体   繁体   中英

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. 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. 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. If someone could explain __name __ with classes better and help me fix the error, I would much much appreciate that.

Here is my 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:

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. The problem is because of passing an instance in the Meta class of RegisterForm instead of the class. So to fix:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM