pytest在创建数据库之前执行代码

时间:2017-04-06 13:21:29

标签: django pytest

public final native double getR_press_delta_pressure() /*-{ return this.r_press_delta_pressure; }-*/; public final native double getR_press_0_delta() /*-{ return this.r_press_0_delta; }-*/; 中,我阻止用户复制记录。

AdminForm

mgmt_users / models.py

from django import forms
from django.contrib.auth.models import User

from apps.accounting_users.models import AccountingUser
from apps.marketing_users.models import MarketingUser
from apps.mgmt_users.models import ManagementUser
from apps.serv_mgrs_users.models import ServiceMgmtUser
from apps.serv_staff_users.models import ServiceStaffUser


class BaseDjangoAdminRoleForm(forms.ModelForm):
    user = forms.ModelChoiceField(
        queryset=User.objects.exclude(
            username__in=[record.user.username for record in AccountingUser.objects.all()] +
                         [record.user.username for record in MarketingUser.objects.all()] +
                         [record.user.username for record in ManagementUser.objects.all()] +
                         [record.user.username for record in ServiceMgmtUser.objects.all()] +
                         [record.user.username for record in ServiceStaffUser.objects.all()],
        )
    )

公地/模型

from apps.commons.models import AbstractSBrandUser


class ManagementUser(AbstractSBrandUser):
    pass

OSX 10.12.4 pytest是正常的 Debian:Jessie破了

class AbstractSBrandUser(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    branch = models.ForeignKey(Branch, on_delete=models.SET_NULL, null=True)
    mobile = models.CharField(max_length=15)

    class Meta:
        abstract = True

问题:
我已经根据 File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/apps.py", line 23, in ready self.module.autodiscover() File "/usr/local/lib/python3.6/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover autodiscover_modules('admin', register_to=site) File "/usr/local/lib/python3.6/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules import_module('%s.%s' % (app_config.name, module_to_search)) File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 978, in _gcd_import File "<frozen importlib._bootstrap>", line 961, in _find_and_load File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 655, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed File "/buddy/siam-sbrand/portal/apps/mgmt_users/admin.py", line 3, in <module> from apps.commons.forms import BaseDjangoAdminRoleForm File "/buddy/siam-sbrand/portal/apps/commons/forms.py", line 11, in <module> class BaseDjangoAdminRoleForm(forms.ModelForm): File "/buddy/siam-sbrand/portal/apps/commons/forms.py", line 17, in BaseDjangoAdminRoleForm [record.user.username for record in ServiceMgmtUser.objects.all()] + File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 256, in __iter__ self._fetch_all() File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 1087, in _fetch_all 检查了语法。
我已经检查了python3的{​​{1}}语法。
filter运行正常。完全没有错误。

如何在Django的Django1.10中使用queryset并通过测试? 我是否需要牺牲此功能的测试?

1 个答案:

答案 0 :(得分:2)

您不能在模型表单定义中使用[record.user.username for record in AccountingUser.objects.all()]之类的查询集。 Django尝试在加载模块时评估查询集。如果尚未创建表,则会出错。

您可以通过在__init__方法中设置查询集来解决问题。

class BaseDjangoAdminRoleForm(forms.ModelForm):
    user = forms.ModelChoiceField(queryset=User.objects.none())

    def __init__(self, *args, **kwargs):
        super(BaseDjangoAdminRoleForm, self).__init__(*args, **kwargs)
        self.fields['user'].queryset = User.objects.exclude(...)

这样做的好处是每次创建表单时都会评估用户名 - 目前,它们仅在服务器启动时评估一次。