加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-如何在类中定义带有值的变量以及如何在其他类中使用

发布时间:2020-12-17 17:41:30 所属栏目:Python 来源:网络整理
导读:我课外有变数.需要变量才能正常使用类.如何在课堂上移动它并在其他课堂上使用它? 这很好用,但是我需要在UserDevice类中移动STATUS_CHOICES并在UserDeviceAdmin中使用STATUS_CHOICES. STATUS_CHOICES = ((0,gettext("disabled")),(1,gettext("allowed")))cla

我课外有变数.需要变量才能正常使用类.如何在课堂上移动它并在其他课堂上使用它?

这很好用,但是我需要在UserDevice类中移动STATUS_CHOICES并在UserDeviceAdmin中使用STATUS_CHOICES.

STATUS_CHOICES = ((0,gettext("disabled")),(1,gettext("allowed")))

class UserDevice(BaseModel):
    """Table with all devices added and owned by users."""

    device_uniqueid = CharField(primary_key=True)
    device_user = ForeignKeyField(User,null=True,backref='userdevices')
    device_name = CharField()
    model = CharField()
    phone = CharField()
    status = IntegerField(choices=STATUS_CHOICES,default=1)
    inserted_at = DateTimeField(null=True)

    def myfunc(self):
        return self.a

class UserDeviceAdmin(ModelView):
    can_create = False
    edit_modal = True
    column_choices = {'status': STATUS_CHOICES}
    column_list = [
        'device_uniqueid','device_user.email','device_name','model','phone','status','inserted_at',]
    column_sortable_list = ('device_uniqueid','device_user.email')
    form_ajax_refs = {'device_user': {'fields': ['email']}}
最佳答案
将其移入内部:

class UserDevice(BaseModel):
    """Table with all devices added and owned by users."""
    STATUS_CHOICES = ((0,gettext("allowed")))

从另一个类访问它:

class UserDeviceAdmin(ModelView):
    can_create = False
    edit_modal = True
    column_choices = {'status': UserDevice.STATUS_CHOICES}

就像静态变量一样.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读