1

I want to add the method has_access_token to a django model ... how can I turn it into a paramater?

meaning if I have the following method in the Person model:

def has_access_token(self):
    return True

person = Person.objects.get(user=user)

I want to be able to do:

if person.has_access_token:

without adding parenthesis: () like person.has_access_token()

4

1 に答える 1

2

使用property:

@property
def has_access_token(self):
    return True

>>> class MyModel:
...     def has_access_token_1(self):
...         return True
...     @property
...     def has_access_token_2(self):
...         return True
... 
>>> 
>>> obj = MyModel()
>>> obj.has_access_token_1
<bound method MyModel.has_access_token_1 of <__main__.MyModel instance at 0x7f07ed881ea8>>
>>> obj.has_access_token_2
True
于 2013-09-18T17:19:31.243 に答える