-3

I was working on a small program and I am getting this NameError. Error is below:

NameError: name 'create_user' is not defined

Following is a short program that I wrote:

class UserManager():
    email = "abc"
    def create_user(email, password=None):
        user = (email, password)

    def create_superuser(email, password):
        user = create_user(
            email,
            password = password
            )
        return ("User is an admin")

password = input("Password: ")

User = UserManager()

print(User.create_superuser(password))
Khubaib Khawar
  • 51
  • 1
  • 11
  • What exactly does this have to do with django? – user2390182 Jul 05 '20 at 09:25
  • 2
    Instance methods on a class have to take a mandatory argument `self` (or any other name), representing the instance calling the method. – Jan Christoph Terasa Jul 05 '20 at 09:26
  • Probably related/ a duplicate of [https://stackoverflow.com/questions/17134653/difference-between-class-and-instance-methods](https://stackoverflow.com/questions/17134653/difference-between-class-and-instance-methods) – Patrick Artner Jul 05 '20 at 09:29
  • 2
    Please study a turorial about classes, f.e. [https://docs.python.org/3/tutorial/classes.html](https://docs.python.org/3/tutorial/classes.html) – Patrick Artner Jul 05 '20 at 09:30
  • 1
    If you want the method to be a static method, put @ staticmethod. If you want it to be a class method put @ classmethod on top of it and add parameter cls. Otherwise, add self parameter. – Pramote Kuacharoen Jul 05 '20 at 09:31
  • @schwobaseggl I am creating a custom user model in Django. – Khubaib Khawar Jul 05 '20 at 10:04

1 Answers1

0

I have amended your code and this would work, although it is not clear what you want to do. As per previous comments here, you need to use self, and init. The code below works but it still has no sense. It's just to make you see why you get an error. Add return to each function and make it clear what each function should do. (Also, it's a bit strange asking the password without the email).

class UserManager:
    def __init__(self):
        self.email = "abc"

    def create_user(self, email, password=None):
        user = (email, password)
        return user

    def create_superuser(self, email, password):
        self.create_user(email, password = password)
        return "User is an admin"

password = input("Password: ")
User = UserManager()
print(User.create_superuser(None, password))
Fab
  • 142
  • 8
  • Yeah, I know the code does not do anything useful, I actually instead of posting whole User Custom Model that I created in Django, just posted a part of it (also edited, to make it look a small normal program) that just prints `User is an admin`. – Khubaib Khawar Jul 05 '20 at 10:07