如何在djang中使用meta类中的类属性

2024-05-12 18:29:06 发布

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

我有这个密码

class DistrictResource(ModelResource):
    model=models.District
    res_name="district"
    class Meta:
        queryset = self.model.District.objects.active()

如果我使用self,当我遇到错误时,如何在meta中使用self.model


Tags: nameself密码modelobjectsmodelsresmeta
1条回答
网友
1楼 · 发布于 2024-05-12 18:29:06

你需要使用

queryset = models.District.objects.active() 

而不是

^{pr2}$

在这种情况下。在

编辑:

由于scope resolution in Python规则,无法访问内部类中的res_name

A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition.

A scope defines the visibility of a name within a block.

The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes generator expressions since they are implemented using a function scope.

关于Python范围解析,一个容易记住的规则是LEGB规则:

L.局部变量,即函数内指定的名称。在

封闭函数局部变量。在

G.全局

B.内置。在


当Django/tastype使用元类构造类时,class Meta被Django/tastype用作配置选项。我不知道为什么要从Meta内部访问Meta外部的变量,而不是仅仅在Meta内部定义它。在

相关问题 更多 >