3

I have variable outside class. Variable is needed for proper function of class. How to move it inside class and use it in other class?

This works good, but I need move STATUS_CHOICES inside class UserDevice and use STATUS_CHOICES inside UserDeviceAdmin too.

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']}}
user13978
  • 113
  • 1
  • 5
  • 1
    Why do you *need* to move `STATUS_CHOICES` into `UserDevice`? – chepner Apr 08 '19 at 16:51
  • I can try to come up with something for you specific case, but I think that your answer can be found [here]( https://stackoverflow.com/questions/11421659/passing-variables-creating-instances-self-the-mechanics-and-usage-of-classes). This is a little complex to read though. – bart cubrich Apr 08 '19 at 16:55
  • It is peewee ORM model definition. I think that it is better to have `STATUS_CHOICES` inside `UserDevice` because `STATUS_CHOICES` is used only in UserDevice` model. – user13978 Apr 08 '19 at 19:54

2 Answers2

2

You should consider using a parent class, from which you can inherit, thanks to multiple inheritance this is easy in 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....

Note, you better change the name of the parent class to something relevant in your Domain

Lucas
  • 395
  • 1
  • 11
  • Thank you so much, but I think that @DroidX86 solution is better for me. But thank you for teaching me. – user13978 Apr 08 '19 at 19:50
0

Move it inside:

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

Access it from the other class:

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

It's like static variables.

rdas
  • 20,604
  • 6
  • 33
  • 46
  • It is peewee ORM model definition and flask-admin view definition. Your solution is best for me, because `STATUS_CHOICES` is used for `UserDevice` model. Thank you so much. – user13978 Apr 08 '19 at 19:48