标签: django-fixtures

使用fixture将django admin auth.groups和用户迁移到新数据库

这是场景:

我正在使用django的管理界面,我希望能够通过fixtures加载用户和组(如果可能的话.)我可以像这样转储用户/组:

manage.py dumpdata auth auth.group > usersandgroups.json
Run Code Online (Sandbox Code Playgroud)

但是在将数据加载到一个全新的数据库中时......

manage.py loaddata <appname>/fixtures/usersandgroups.json
Run Code Online (Sandbox Code Playgroud)

我得到了与外键等有关的各种错误.这是一个例子:

django.db.utils.IntegrityError: insert or update on table "auth_permission" violates foreign key constraint "content_type_id_refs_id_728de91f"

DETAIL: Key (content_type_id)=(37) is not present in table "django_content_type".
Run Code Online (Sandbox Code Playgroud)

如果有人能指出我正确的方向,我真的很感激.提前致谢!

django postgresql django-admin django-manage.py django-fixtures

8
推荐指数
1
解决办法
4363
查看次数

如何在Django 1.7+中创建每个项目的initial_data fixture

在Django 1.7之前,我曾经fixtures在设置中定义了每个项目目录:

FIXTURE_DIRS = ('myproject/fixtures',)
Run Code Online (Sandbox Code Playgroud)

并使用它来放置我的initial_data.json夹具存储整个项目必不可少的默认.这对我来说效果很好,因为我可以通过将每个项目数据与特定于应用程序的数据分开来保持设计的清洁.

现在使用Django 1.7,initial_data不推荐使用夹具,建议数据迁移与应用程序的架构迁移结合在一起; 没有明显的选择全球每个项目的初始数据.

此外,新的迁移框架执行兼容应用程序(包括django.contrib.auth应用程序)的迁移之前安装所有旧的初始数据夹具.此行为导致我的包含默认组的fixture 无法安装,因为该auth_group表尚未存在于DB中.

有关如何(优雅地)所有迁移之后(或至少在auth应用程序迁移之后)运行灯具的建议?还是其他任何想法来解决这个问题?我发现夹具是提供初始数据的好方法,并希望有一种简单而干净的方式来声明它们的自动安装.新的RunPython太麻烦了,我认为它对大多数用途来说都是一种过度杀伤力; 它似乎只适用于每个应用程序的迁移.

django django-fixtures django-1.7 django-migrations

8
推荐指数
1
解决办法
1584
查看次数

测试时,Django夹具的加载顺序是否正确?

我正在测试我的应用程序,我遇到了一个问题,我不知道为什么.我正在为我的测试加载灯具,灯具有相互依赖的外键.它们必须按特定顺序加载,否则将无法运行.

我正在加载的灯具是:

["test_company_data", "test_rate_index", 'test_rate_description']

公司数据是第一个.test_rate_index具有公司的外键,test_rate_description具有test_rate_index中声明的模型的外键.(顺便说一句,不同的测试需要不同的装置,这就是为什么我不只是将所有东西都推到一起)

如果我使用django的标准过程来加载测试,则测试不会以正确的顺序加载.

class TestPackages(test.TestCase):
    fixtures = ["test_company_data", "test_rate_index", "test_rate_description",]

我收到了消息

DoesNotExist: RateDescription matching query does not exist.

但是,如果我颠倒我的灯具的顺序(这没有任何意义)它的工作原理:

fixtures = ["test_rate_description", "test_company_data", "test_rate_index",]

Django的文档声明夹具按照声明的顺序加载,但似乎并非如此.

作为一种解决方法,而不是使用django的

    call_command('loaddata', *fixtures, **{
                                            'verbosity': 0,
                                            'commit': False,
                                            'database': 'default'
                                         })

我在setUp方法中使用了一个不同的函数,一次加载一个fixture.

def load_fixtures(fixtures):
    for fixture in fixtures:
        call_command('loaddata', fixture, **{
                                            'verbosity': 0,
                                            'commit': False,
                                            'database': 'default'
                                            })

在尝试使用标准方法时,是否有一些我正在做错误或不理解导致我的灯具没有以正确的顺序加载?

python django django-fixtures

7
推荐指数
1
解决办法
1873
查看次数

Django,通用关系,制作固定装置

我正在尝试为django-test-utils makefixture命令添加泛型关系和一对一关系支持,这里是源代码http://github.com/ericholscher/django-test-utils/blob/master/test_utils /management/commands/makefixture.py

有人有想法如何做到这一点?或者可能有另一种工具用于:

./manage.py dumpcmd User[:10] > fixtures.json
Run Code Online (Sandbox Code Playgroud)

django fixtures django-testing django-fixtures

7
推荐指数
1
解决办法
611
查看次数

在加载Django灯具时没有调用model.save()?

我重写了我的Django模型save()方法,所以我可以对该对象进行一些额外的健全性检查.(save()是否正确执行此操作?)

看来我的fixtures/initial_fixtures.yaml对象没有调用它们的save()方法.我怎样才能理智地检查我的装置?

testing django fixtures sanity-check django-fixtures

7
推荐指数
2
解决办法
1186
查看次数

Django manage.py测试无法正确加载夹具

我使用django.test.TestCase编写了Django测试,我想使用一个包含所有当前数据库数据的fixture来运行测试.但是,如果我按如下方式创建夹具:

python manage.py dumpdata --indent=3 > myapp/fixtures/test_data.json
Run Code Online (Sandbox Code Playgroud)

当我然后使用运行测试时python manage.py test myapp,我收到以下错误:

Problem installing fixture...(traceback)
IntegrityError: Could not load auth.Permission(pk=42): duplicate key value violates unique constraint "auth_permission_content_type_id_codename_key"
DETAIL:  Key (content_type_id, codename)=(14, add_record) already exists.
Run Code Online (Sandbox Code Playgroud)

我在某处看到这可能是由pk冲突造成的,所以我尝试用以下方法重新创建灯具:

python manage.py dumpdata --natural --indent=3 > myapp/fixtures/test_data.json
Run Code Online (Sandbox Code Playgroud)

但现在运行测试给了我:

Problem installing fixture...(traceback)
DeserializationError: 'NoneType' object has no attribute '_meta'
Run Code Online (Sandbox Code Playgroud)

我也试过各种排除(使用--exclude选项)auth.permissioncontenttypes(或同时),但后来我抱怨缺少权限(Key (permission_id)=(44) is not present in table "auth_permission".)或缺少内容类型(DeserializationError: ContentType matching query does not exist.)

问题是,我还是需要权限,因为我的测试部分是为了验证只有具有特定权限的用户才能访问某些视图.

我不明白为什么会发生这种情况,说实话 …

django unit-testing django-testing django-fixtures

7
推荐指数
1
解决办法
3748
查看次数

Django 1.4:如何忽略loaddata中的字段和模型

我们有一个15MB上传的压缩转储数据提取到100MB.它几乎没有在当前表中使用的模型和字段.

具体来说,contenttypes可以忽略的模型很少,并且field_在模型中可以忽略一个字段.

Django 1.5用户-ignorenonexistent可以安全地完成忽略工作.但是如何在1.4中有效地做到这一点?

python django django-models django-fixtures

7
推荐指数
1
解决办法
883
查看次数

将信息从Scrapy爬虫连续导出到Django应用程序数据库的最佳方法是什么?

我正在尝试构建一个类似于商店的Django应用程序.从互联网上删除项目,并随着时间的推移不断更新Django项目数据库(比如每隔几天).我正在使用Scrapy框架来执行抓取,虽然有一个实验性的DjangoItem功能,但我宁愿远离它,因为它不稳定.

现在我的计划是使用Scrapy XMLItemExporter(此处为 docs )创建已爬行项目的XML文件,并将这些loaddata文件用作XML fixture(这里的文档)进入Django项目.这似乎没问题,因为如果两个进程中的一个搞砸了,它们之间就有一个文件中介.模拟整个应用程序似乎也不是一个坏主意.

一些担忧是:

  • 这些文件可能太大而无法读入Django的内存loaddata.
  • 当我可能有更好或更简单的解决方案时,例如直接导出到数据库,在这种情况下是MySQL,我花费了太多时间.
  • 似乎没有人在网上写过这个过程,这很奇怪,考虑到Scrapy是我认为插入Django应用程序的优秀框架.
  • 没有关于在Django的文档上手动创建Django灯具的明确指南 - 看起来它更倾向于从应用程序本身转储和重新加载灯具.

实验性DjangoItem的存在表明Scrapy + Django是一个很受欢迎的选择,因为这里有一个很好的解决方案.

我非常感谢有关此事的任何解决方案,建议或智慧.

django production-environment scrapy django-fixtures

6
推荐指数
1
解决办法
1251
查看次数

Django中的夹具用South/Selenium测试

我正在尝试在使用South的Django项目(1.5.4)上运行Selenium测试.当我尝试用夹具注入初始数据时,我认为South与我的测试相冲突,但我不确定为什么; 我感谢任何帮助.

根据Django 文档,应该在第一个syncdb之后加载fixture,然后应用所有迁移.

问题1)这是否考虑到南迁?我需要以某种方式单独运行它们吗?

我运行测试时得到的错误使我看起来在第一次测试后我的南迁移仍然存在于测试数据库中...但我认为每个测试都有自己的数据库(和迁移/固定装置)?第一个测试通过/失败,但每个后续测试都会引发此IntegrityError:

IntegrityError: Problem installing fixture '<PROJECT_PATH>/fixtures/toy_course.json': Could not load contenttypes.ContentType(pk=8): (1062, "Duplicate entry 'south-migrationhistory' for key 'app_label'")
Run Code Online (Sandbox Code Playgroud)

这个南方文档SO问题似乎表明我需要覆盖某种类型的前方法才能使夹具工作,但我不完全确定如何将其应用于测试情况而不是生产(或者如果是我需要的解决方案).

问题2)我是否应该在测试设置中覆盖前进?我会在哪里做的?

我的相关测试代码:

from django.conf import settings

from selenium import webdriver

from functional_tests.test import SeleniumTestCase

class Resources(SeleniumTestCase):
    fixtures = ['toy_course.json']

    def setUp(self):      
        self.browser = webdriver.Chrome(settings.SELENIUM_WEBDRIVER)
        self.browser.implicitly_wait(3)

    def tearDown(self):
        self.browser.quit()

    def test_main_page_renders_correctly(self):
        """
        User sees a properly formatted main page
        """
        self.open('/RDB/')

        h3_headers = self.browser.find_elements_by_tag_name('h3')
        self.assertIn(
                'Complete List of Resources', …
Run Code Online (Sandbox Code Playgroud)

django selenium unit-testing django-south django-fixtures

6
推荐指数
1
解决办法
1235
查看次数

如何使用 pytest-django 在每个会话中仅创建一次用户对象?

首先,我厌倦了这个:

\n
@pytest.mark.django_db\n@pytest.fixture(scope='session')\ndef created_user(django_db_blocker):\n    with django_db_blocker.unblock():\n        return CustomUser.objects.create_user("User", "UserPassword")\n\ndef test_api_create(created_user):\n    user = created_user()\n    assert user is not None\n
Run Code Online (Sandbox Code Playgroud)\n

但我得到了一个UndefinedTable错误。因此,以某种方式标记我的装置并@pytest.mark.django_db没有\xe2\x80\x99t 实际上注册了我的 Django DB。接下来我尝试将 db 对象直接传递给固定装置:

\n
@pytest.fixture(scope='session')\ndef created_user(db, django_db_blocker):\n    with django_db_blocker.unblock():\n        return CustomUser.objects.create_user("User", "UserPassword")\n\ndef test_api_create(created_user):\n    user = created_user()\n    assert user is not None\n
Run Code Online (Sandbox Code Playgroud)\n

但后来我得到了一个错误

\n
ScopeMismatch: You tried to access the 'function' scoped fixture 'db' with a 'session' scoped request object, involved factories\n
Run Code Online (Sandbox Code Playgroud)\n

最后,为了确认一切正常,我尝试了:

\n
@pytest.fixture\ndef created_user(db, django_db_blocker):\n    with django_db_blocker.unblock():\n        return CustomUser.objects.create_user("User", "UserPassword")\n\ndef test_api_create(created_user):\n …
Run Code Online (Sandbox Code Playgroud)

python fixtures pytest django-fixtures pytest-django

6
推荐指数
1
解决办法
1626
查看次数