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

前端之家收集整理的这篇文章主要介绍了python-如何在类中定义带有值的变量以及如何在其他类中使用 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

这很好用,但是我需要在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}

就像静态变量一样.

原文链接:https://www.f2er.com/python/533299.html

猜你在找的Python相关文章