0

I've created two new methods in models.py:

def test(self):
     return 'Test'

However, after restarting apache and wsgi, I'm unable to use the methods in templates or in command line.

Instead I get 'Model doesn't have attribute 'test'

Has anyone seen this issue before? How did you resolve?

class ClientAppointmentMessage(models.Model):
    # fields here

    class Meta:
        ordering = ['-created']

    def __unicode__(self):
        return self.appointment.creator.name

    def get_absolute_url(self):
        return '/appointment/{0}'.format(self.appointment.id)

    def test(self):
        return 'Test'

NOTES:

  • Template ignores the function completely (no errors, no result)

  • Shell throws 'ClientAppointmentMessage' has no attribute 'test'

  • I've ensured that I'm uploading file to correct directory.

  • I've tried '@property' above the method

  • I've restarted apache and wsgi

Example of shell commands:

from path.to.models import ClientAppointment, ClientAppointmentMessage
a = ClientAppointment.objects.get(id=1)
a.test() --> AttributeError: 'ClientAppointment has no attribute 'test''
a = ClientAppointmentMessage.objects.get(id=1)
a.test() --> AttributeError: 'ClientAppointmentMessage' has no attribute 'test'
a = ClientAppointmentMessage()
dir(a) --> shows list of methods, not including 'test' (this occurs both as property and as regular method) 
Craig
  • 402
  • 1
  • 4
  • 15
  • This method in in your models file or in a model class? BTW, try with `service apache2 reload` – Gocht Jan 21 '16 at 15:46
  • Method is in the model class. No dice on apache2 reload. It's strange, I've pulled out any .pyc files that may have to been cached. – Craig Jan 21 '16 at 15:50
  • Please show the method in context, and some examples of how you are actually using it. – Daniel Roseman Jan 21 '16 at 15:50
  • add @property above your method and try again – Bogdan Goie Jan 21 '16 at 15:53
  • No change with '@property' – Craig Jan 21 '16 at 15:59
  • Are you sure that you are calling the test method/property in template on a ClientAppointmentMessage instance? can you paste a snipet of the django template in question? – Bogdan Goie Jan 21 '16 at 16:22
  • The template doesn't really matter if I can't get the shell to call the method/property. If the shell throws an AttributeError, the template definitely won't work, either. – Craig Jan 21 '16 at 16:40
  • @Craig one last question..did you quit and re-entered the python interpreter manage.py shell (or shell_plus..whichever you use) instance after making the changes in order to test the commands posted in the example above? – Bogdan Goie Jan 21 '16 at 16:54
  • Yup. That's what is so mystifying to me... – Craig Jan 21 '16 at 17:43
  • Could you possibly have a mixture of tabs and spaces, which might make the test method look less indented than it actually is compare to the other ones? – Daniel Roseman Jan 21 '16 at 20:44
  • WOW! @DanielRoseman - That's it! I can't believe it. Normally when I have space/tab issues I get indentation errors. Thanks! You should put that in an answer and I'll mark it as the solution. – Craig Jan 22 '16 at 16:37

1 Answers1

0

The issue was that there was a mix of spaces/tabs in the models.py file. What is strange, however, is that normally that will trigger an indentation error. Not sure why this one didn't, but for some who may be having similarly unexplainable issues, it's worth checking.

Craig
  • 402
  • 1
  • 4
  • 15