I am newbie at Django. I have model with a custom method. In view I am
retrieving a single 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('/')
in view I am doing this
def show(request):
problem = Problem.objects.all()[0]
t1=problem.retrieve_rankdata()
But, I am getting this error
‘Problem’ object has no attribute ‘retrieve_rankdata’
What am I doing wrong?
It’s a bit hard to tell, but the problem could be with your indentation. The indentation in the code you’ve pasted is inconsistent, so it wouldn’t work properly anyway – I suspect in your actual code,
def retrieve_rankdatais a couple more spaces to the left.Don’t forget that Python uses indentation to tell whether or not an attribute or a method is part of a class. So if your
def retrieve_rankdataline is not actually indented at the same level as thedef __unicode__one, it won’t be considered part of theProblemclass.