Can't call custom method

I am new to Django. I have a model with a custom method. Meaning that I am getting one object.

Here is my code - My model

class Problem(models.Model): 
    problem = models.CharField(max_length=100) 
    solution=models.CharField(max_length=500) 
    def __unicode__(self): 
        return self.problem 
   def retrieve_rankdata(self): 
                return self.problem.split()[0].split('/') 

      

Mind I do this

def show(request): 
       problem = Problem.objects.all()[0] 
        t1=problem.retrieve_rankdata() 

      

But I am getting this error Object "Problem" has no attribute "retrieve_rankdata"

What am I doing wrong?

0


a source to share


1 answer


It's a little tricky to tell, but the problem might be with your indentation. The indentation in the pasted code is incompatible, so it won't work as expected - I suspect def retrieve_rankdata

there are some more spaces to the left in your actual code .



Remember that Python uses indentation to determine if an attribute or method is part of a class. Therefore, if your line def retrieve_rankdata

is not actually indented at the same level as def __unicode__

, it will not be considered part of the class Problem

.

+1


a source







All Articles