So, yesterday I posted a problem with a class method I was having (Can an object method return an instance of the object?) and the answer was "You can change it to a class method if you like, but there's no reason your code shouldn't work as it is." Changing it to a class method worked, so I moved on with my life. Now I'm having another problem where the below method (same class) didn't work, telling me that 'NoneType' object has no attribute 'objects'
. I jumped into the debugger, and the exact same code worked fine when entered manually. I moved the import User
line from the top of the file into the function itself, and now it works. This is the only place in the file where the User class is used. Person and User are not linked with a foreign key relationship--they just share a username and we do the lookup that way.
I should also add that both of these methods worked fine up until a few days ago, and I haven't been able to find any alterations of them in our commit history.
Class Person(models.Model):
def to_user(self):
'''returns user linked to Person or None(needed for some third
party packages)'''
try:
from django.contrib.auth.models import User
return User.objects.get(username=str(self.username))
except ObjectDoesNotExist:
print("No associated user found using username {0}".format(self.username))
return None