Home > front end >  TypeError: all() missing 1 required positional argument: 'self' while using AbstractUser m
TypeError: all() missing 1 required positional argument: 'self' while using AbstractUser m

Time:07-26

class User(AbstractUser):
    nickname = models.CharField(max_length=32)
    birthday = models.DateField()
    objects = MyUserManager

class Meta:
    ordering = ('-id',)

I created an custom user model and defined the MyUserManager class to manage the model.

class MyUserManager(UserManager):
    def _create_user(self, username, email, password, **kwargs):
        if not username:
            raise ValueError("The given username must be set")
        user = self.model(email=self.normalize_email(email), **kwargs)
        user.set_password(password)
        user.save()
        return user

    def create_user(self, username, email=None, password=None, **kwargs):
        kwargs.setdefault('is_admin', False)
        return self._create_user(username, email, password, **kwargs)

    def create_superuser(self, username, email=None, password=None, **kwargs):
        kwargs.setdefault("is_staff", True)
        kwargs.setdefault('is_admin', True)
        return self._create_user(username, email, password, **kwargs)

I was going to create a createuser/createsuperuser function that receives values for the custom fields as parameters. However, first of all, I would like to resolve the errors that have occurred now.

class UserViewSet(ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializers

This is the whole error code.

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 134, in inner_run
    self.check(display_num_errors=True)
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/core/management/base.py", line 487, in check
    all_issues = checks.run_checks(
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/core/checks/urls.py", line 14, in check_url_config
    return check_resolver(resolver)
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/core/checks/urls.py", line 24, in check_resolver
    return check_method()
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/urls/resolvers.py", line 480, in check
    for pattern in self.url_patterns:
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/utils/functional.py", line 49, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/urls/resolvers.py", line 696, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/utils/functional.py", line 49, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/urls/resolvers.py", line 689, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/choisemin/Desktop/workspace/my_memories/my_memories/my_memories/urls.py", line 21, in <module>
    path('',include('account.urls')),
  File "/Users/choisemin/Desktop/workspace/my_memories/myvenv/lib/python3.8/site-packages/django/urls/conf.py", line 38, in include
    urlconf_module = import_module(urlconf_module)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/choisemin/Desktop/workspace/my_memories/my_memories/account/urls.py", line 2, in <module>
    from . import views
  File "/Users/choisemin/Desktop/workspace/my_memories/my_memories/account/views.py", line 7, in <module>
    class UserViewSet(ModelViewSet):
  File "/Users/choisemin/Desktop/workspace/my_memories/my_memories/account/views.py", line 8, in UserViewSet
    queryset = User.objects.all()
TypeError: all() missing 1 required positional argument: 'self'

This is views.py in account app and error have occured in this code. Why can't I find a self instance? What instance does the original User.objects return? Is there a problem with the MyUserManager class?

CodePudding user response:

Try adding parentheses in front of myusermanager in your user model like:

objects = MyUserManager()
  • Related