gpt4 book ai didi

django 1.8 测试模型和迁移

转载 作者:行者123 更新时间:2023-12-03 17:47:46 24 4
gpt4 key购买 nike

我使用 django 1.8
我有一个引用 django.contrib.contenttypes.ContentType 的模型:

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType


class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')

def __str__(self): # __unicode__ on Python 2
return self.tag

我有一个tests.py:
class TestObj(models.Model):
value = models.IntegerField(default=42, null=False)

def __str__(self):
text = "%s-%i" % ("name", self.value)
return text


class MyUnitTest(TestCase):
def setUp(self):
TestObj.objects.create(value=40)
TestObj.objects.create()

def test_my_test(self):
obj1 = TestObj.objects.get(value=40)
obj2 = TestObj.objects.get(value=42)
self.assertEqual(obj1.value, 40)
self.assertEqual(obj2.value, 42)

当我尝试在没有迁移的情况下创建数据库(以及“测试”)时,我收到一个无效引用的错误:
./manage.py syncdb
...
django.db.utils.ProgrammingError: ERROR: reference "django_content_type" does not exist

但是如果我创建一个迁移(./manage.py makemigrations myobj),迁移只包含一个models.py模型(TaggedItem):
Migrations for 'myobj':
0001_initial.py:
- Create model TaggedItem

在我尝试测试我的应用程序后,我收到一个错误,引用 myob_testobj 不存在:
./manage.py test -v3
Creating test database for alias 'default' ('testdb_49308_4288843')...
Operations to perform:
Synchronize unmigrated apps: staticfiles
Apply all migrations: myobj, contenttypes, sessions
Synchronizing apps without migrations:
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application myobj
Creating tables...
Running deferred SQL...
Installing custom SQL...
Loading 'initial_data' fixtures...
Checking 'mypro' for fixtures...
No fixture 'initial_data' in 'mypro'.
Installed 0 object(s) from 0 fixture(s)
Running migrations:
Rendering model states... DONE (0.010s)
Applying contenttypes.0001_initial... OK (0.133s)
Applying contenttypes.0002_remove_content_type_name... OK (0.017s)
Applying myobj.0001_initial... OK (0.217s)
Applying sessions.0001_initial... OK (0.233s)
Running post-migrate handlers for application contenttypes
Adding content type 'contenttypes | contenttype'
Running post-migrate handlers for application sessions
Adding content type 'sessions | session'
Running post-migrate handlers for application myobj
Adding content type 'myobj | taggeditem'
Adding content type 'myobj | testobj'
Traceback
...
django.db.utils.ProgrammingError: ERROR: reference "myobj_testobj" does not exist

如果我不使用迁移并运行测试,则会创建表 myobj_testobj 但我收到一个错误,没有引用“django_content_type”
/manage.py test -v3
Creating test database for alias 'default' ('testdb_773982_6463361')...
Operations to perform:
Synchronize unmigrated apps: staticfiles, myobj
Apply all migrations: contenttypes, sessions
Synchronizing apps without migrations:
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application myobj
Creating tables...
Creating table myobj_taggeditem
**Creating table myobj_testobj**
Running deferred SQL...
Traceback
...
django.db.utils.ProgrammingError: ERROR: reference "django_content_type" does not exist

如何对模型使用测试?
谢谢你。

最佳答案

Django 无法发现模型 TestObj因为您的 test.py 不是应用程序,并且没有任何已安装的应用程序描述 TestObjsettings.INSTALLED_APPS .您需要添加型号TestObj转模块 myobj.models .

之后运行 python manage.py makemigrations myobj然后将创建 TestObj 的迁移。所以你可以运行你的测试。

关于django 1.8 测试模型和迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29772561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com