Django管理员中的查询集破坏 migrations 和 py.tes

2024-09-27 00:15:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我在加载过程中得到FATAL: database "xxxxxxxxx" does not existpy.测试. 这来自于管理表单的加载,其中我将一个queryset传递给ModelChoiceField

class CustomChoiceField(forms.ModelChoiceField):
    def __init__(self, queryset=None, *args, **kwargs):
        return super().__init__(SomeModel.objects.limited_qs(), *args, **kwargs)


class SomeModelForm(forms.ModelForm):
    some_model = CustomChoiceField()

    class Meta:
        model = SomeModel


class SomeModelAdmin(admin.ModelAdmin):
    form = SomeModelForm

我试过了

^{pr2}$

所有的结果都与py.测试在

编辑

__init__方法或formfield_overrides中使用queryset也会破坏迁移。在

注意:这些都是我希望它们在服务器上的工作方式-它们只是阻止我运行任何测试。在

我如何让它通过(不需要有条件地注册我的管理资料;\uI like tdd)


Tags: modelinit过程argsformsdatabasekwargsclass
1条回答
网友
1楼 · 发布于 2024-09-27 00:15:22

我也有类似的问题,在我的情况下,问题是模型对象有限公司(),我给另一个queryset打了个电话:

self.filter(another_model=AnotherModel.objects.get(xxx=1))

因此,为了构建查询集,在创建测试数据库之前执行了对数据库的调用,导致“致命数据库不存在”

相关问题 更多 >

    热门问题