Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django model property with parameter

I've the following models in Django.

class User(models.Model):
    name = models.CharField(max_length=50)
    ...
    ...

    @property
    def get_info(self, key=None):
        value = self.name if key else 'Hello World'
        return value

But when I try to execute the code in Django shell, I'm getting the following error.

n [4]: user = User.objects.get(id=1)
n [5]: user.get_info(key='test_key')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-f7b917070aee> in <module>()
----> 1 user.get_info(key='test_key')

TypeError: _get_info() takes exactly 2 arguments (1 given)
like image 511
Pattu Avatar asked Jan 25 '18 06:01

Pattu


1 Answers

Just remove the @property decorator. If you need to accept arguments, it's not a property.

like image 193
wim Avatar answered Oct 04 '22 16:10

wim