我在课外有变数。需要变量才能正常使用类。如何在课堂上移动它并在其他课堂上使用它?
这很好用,但是我需要在类STATUS_CHOICES
内移动UserDevice
并在STATUS_CHOICES
内使用UserDeviceAdmin
。
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']}}
答案 0 :(得分:2)
您应该考虑使用可以从其继承的父类,这要归功于多重继承,这在python中很容易:
class Parent_class(object):
def get_status_choices(self):
return ((0, gettext("disabled")), (1, gettext("allowed")))
class UserDevice(BaseModel, Parent_class):
# your implementation...
status = IntegerField(choices=self.get_status_choices(), default=1)
# and further implementations....
class UserDeviceAdmin(ModelView, Parent_class):
# your implementation...
column_choices = {'status': self.get_status_choices()}
# and further implementations....
请注意,最好将父类的名称更改为与您的域相关的名称
答案 1 :(得分:0)
将其移入内部:
class UserDevice(BaseModel):
"""Table with all devices added and owned by users."""
STATUS_CHOICES = ((0, gettext("disabled")), (1, gettext("allowed")))
从另一个类访问它:
class UserDeviceAdmin(ModelView):
can_create = False
edit_modal = True
column_choices = {'status': UserDevice.STATUS_CHOICES}
这就像静态变量。