I am working on a Django project on the IntelliJ IDEA IDE. I am writing unit tests in the test.py
file for the User
model class. When running the test.py
file with the command python manage.py test
it throws 2 errors on the test_username_cannot_be_blank
method and the test_valid_user method. The error is the same for both methods
AttributeError: 'User' object has no attribute 'fullclean'.
IDEA is also giving me a Instance attribute user defined outside init warnig on line 9 of the test.py file ("self.user = User.objects.create_user()") is where the compiler warning occurs.
What exactly is causing of the AttributeError
and the compiler warning?
# test.py file
from django.test import TestCase
from django.core.exceptions import ValidationError
from .models import User
class UserModelTestCase(TestCase):
def setUp(self):
self.user = User.objects.create_user(
'@johndoe',
first_name='John',
last_name='Doe',
email='[email protected]',
password='Password123',
bio='The quick brown fox jumps over the lazy dog.'
)
def test_valid_user(self):
self._assert_user_is_valid()
def test_username_cannot_be_blank(self):
self.user.username = ''
self._assert_user_is_invalid()
def _assert_user_is_valid(self):
try:
self.user.fullclean()
except ValidationError:
self.fail('user should be valid')
def _assert_user_is_invalid(self):
with self.assertRaises(ValidationError):
self.user.fullclean()
# models.py file
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
bio = models.TextField()
Looked on stackoverflow but could'nt find any solution to the problem.
CodePudding user response:
You have a typo in the method name. It is full_clean
.
CodePudding user response:
I've never even used Django before, but in 3 seconds of googling I was able to determine that the function is full_clean