我无法使用Django运行单元测试。
根据Django文档:
https://docs.djangoproject.com/en/2.0/topics/testing/advanced/#using-different-testing-frameworks
默认的Django测试行为涉及:
我随时尝试运行
./manage.py test
Django到第3步,创建测试数据库,然后抛出错误
django.db.utils.ProgrammingError: relation "accounts_user" does not exist
似乎无法将我的模型迁移到测试数据库。由于它崩溃,它不会删除测试数据库。我可以psql进入测试数据库并看到它存在但它是空的。即使我没有写出任何测试,也会出现这种情况。
我不知道这是否是罪魁祸首,但我确实有一个定义如下的自定义用户模型
def user_image_path(instance, filename):
'''
https://docs.djangoproject.com/en/1.11/ref/models/fields/#django.db.models.FileField.upload_to
'''
first_name = instance.first_name.lower().replace(" ", "_")
last_name = instance.last_name.lower().replace(" ", "_")
cleaned_filename = filename.lower().replace(" ", "_")
return '{0}_{1}/images/avatar/{2}'.format(first_name, last_name, cleaned_filename)
class UserManager(BaseUserManager):
def create_user(self, email, first_name, last_name, username=None, password=None):
if not email:
raise ValueError("Users must have a valid email address")
if not first_name and last_name:
raise ValueError("Users must have a first and last name")
user = self.model(
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name,
)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, first_name, last_name, password):
user = self.create_user(
email=email,
first_name=first_name,
last_name=last_name,
password=password,
)
user.is_staff = True
user.is_admin = True
user.is_superuser = True
user.save()
return user
class User(AbstractBaseUser, PermissionsMixin):
'''
'''
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=40, blank=False)
last_name = models.CharField(max_length=40, blank=False)
avatar = models.ImageField(blank=True, null=True, upload_to=user_image_path)
phone_number = models.CharField(max_length=15, blank=True, null=True)
address_1 = models.CharField(max_length=128, blank=True, null=True)
address_2 = models.CharField(max_length=128, blank=True, null=True)
city = models.CharField(max_length=64, blank=True, null=True)
YOUR_STATE_CHOICES = list(STATE_CHOICES)
YOUR_STATE_CHOICES.insert(0, ('', '---------'))
state = USStateField(blank=True, null=True, choices=YOUR_STATE_CHOICES)
zip_code = models.CharField(max_length=5, blank=True, null=True)
stripe_id = models.CharField(max_length=255, blank=True, null=True)
email_user = models.EmailField(unique=True)
date_joined = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name','last_name',]
def __str__(self):
return "{} @{}, {}".format(self.email, self.last_name, self.first_name)
def get_short_name(self):
return self.last_name
def get_full_name(self):
return ' '.join([self.first_name, self.last_name])
def email_user(self, *args, **kwargs):
send_mail(
'{}'.format(args[0]),
'{}'.format(args[1]),
'support@example.com',
[self.email],
fail_silently=False,
)
我还安装了django-registration和几个使用
的模型models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.CASCADE)
我不确定自定义用户模型或ForeignKey引用是否导致它是否翻转,但自定义用户模型是我的应用程序wrt'stock'Django行为的唯一不寻常的事情。
任何见解都将受到赞赏。
由于
P.S。我正在使用Django 2.0.1。 PostgreSQL的。 macOS 10.13.2。
依赖列表如下
appdirs==1.4.3
beautifulsoup4==4.6.0
boto3==1.5.18
botocore==1.8.32
certifi==2018.1.18
chardet==3.0.4
confusable-homoglyphs==2.0.2
coverage==4.4.2
Django==2.0.1
django-appconf==1.0.2
django-bootstrap4==0.0.5
django-classy-tags==0.8.0
django-extensions==1.9.9
django-localflavor==2.0
django-registration==2.3
django-storages==1.6.5
djangorestframework==3.7.7
docutils==0.14
idna==2.6
jmespath==0.9.3
lxml==4.1.1
olefile==0.44
packaging==16.8
Pillow==5.0.0
psycopg2==2.7.3.2
pyparsing==2.2.0
python-dateutil==2.6.1
pytz==2017.3
requests==2.18.4
s3transfer==0.1.12
six==1.11.0
stripe==1.77.1
typing==3.6.2
urllib3==1.22