4

I'm having issues getting a class method to run in Flask.

In models/User.py:

from mongoengine import *

class User(Document):
  first_name = StringField()
  last_name = StringField()
  ...

  def __init__(self, arg1, arg2, ...):
    self.first_name = arg1
    self.last_name = arg2
    ...

  @classmethod
  def create(self, arg1, arg2, ...):
    #do some things like salting and hashing passwords...
    user = self(arg1, arg2, ...)
    user.save()
    return user

In the main application python file:

from models import User
...
def func():
  ...

  #Throws "AttributeError: type object 'User' has no attribute 'create'"
  user = User.create(arg1, arg2, ...) 

Shouldn't I be able to call create on the User class without instantiating a User object? I'm using Python 2.7.2, and I also tried the non-decorator syntax of using create = classmethod(create), but that didn't work. Thanks in advance!

EDIT: I found one issue: that the models folder did not contain an __init__.py file, so it wasn't a module, so from models import User was not actually importing the file I wanted it to. It did not give me an error from before because I used to have a models.py module in the same directory as the application python script, but after deleting it I never deleted the corresponding .pyc file. Now, I'm getting the error AttributeError: 'module' object has no attribute 'create' instead of what I had before, but I'm certain it is importing the correct file now.

EDIT2: Solved. I then changed the import to from models.User import User and It's hitting the method now.

4
  • 1
    What is User(arg1, arg2, ...): #constructor? I don't think this is valid python syntax. Commented Aug 8, 2012 at 9:19
  • you must read docs before programming! Commented Aug 8, 2012 at 9:20
  • 1
    Try posting a minimal but complete sample of code that actually shows the problem you are getting. What you've done is paraphrase your code introducing new errors in the process and made it impossible for people to help you. Commented Aug 8, 2012 at 9:39
  • Yeah, sorry I was pretty tired when I wrote this and looking back at what I had written it made little sense. Edited the post to more accurately reflect my code. Commented Aug 9, 2012 at 18:47

2 Answers 2

3

The issue was twofold:

  1. The User.py file was in the models/ folder, meaning that my import was actually looking for the User class in the models.py file, which no longer existed but still was being imported without error because the models.pyc file was still around
  2. The import was incorrect for importing within a directory. it should have been from models.User import User, so long as the models/ folder is a module, so all I needed to do then was touch models/__init__.py.
Sign up to request clarification or add additional context in comments.

2 Comments

Cool, this makes sense and it saved me quite a bit of time as I had the same problem with stagnant pyc files. And you're totally right to defend your question and to clarify that you already looked at the obvious solutions.
Yeah, "read the docs" isn't a very helpful answer—I wouldn't be asking on Stack Overflow if I hadn't! :)
1
>>> class foo(object):
...     def __init__(self):
...             pass
...     @classmethod
...     def classmethod(cls):
...             return 0
...
>>> a = foo()
>>> a.classmethod()
0
>>>

4 Comments

Sorry, my question was pretty badly written. This already was done, but class methods still don't work.
@OmarDiab if you using classmethod decorator your first argument should be cls not self docs.python.org/library/functions.html#classmethod
I tried using cls and I tried using self. Both give me the same error. I read the documentation on this, and I don't see where my code is going wrong. Which is why I'm asking on Stack Overflow. I'm sorry if I'm offending you somehow, but the reason I'm asking this here is because I don't see how I'm straying from the documentation, and your sass isn't helping me solve anything. If it means anything I wrote this in terms of generic Foo and Bar classes with no dependencies on other modules and it did work. So I guess I'll play with this more but if anyone has insights I'd love to hear.
@ZagorulkinDmitry Using cls versus self is important for sticking with convention, but doesn't explain the error. Also, your example has non-conventional casing and indentation, and doesn't illustrate the use of cls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.