There are quite a few mistakes in the code. Apart from this, the methods defined in a model class can be called on any instance of the model. In fact, a Model
is a normal python class.
You need to modify the code as below:
#models.py
class MyClass(models.Model):
title = models.CharField(max_length=255, null=False)
body = models.TextField(max_length=255, null=False)
def body_formatted(self):
return "formatted string {0}".format(self.body)
#views.py
def myview(request):
# Get a model instance corresponding to the first DB row
obj = MyClass.objects.first()
body_formatted = obj.body_formatted() # Calling the model's method
return HttpResponse(body_formatted)