Django错误模型.py

2024-10-01 17:33:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,当您创建类和同步数据库时,出现以下错误。在

(DjangoAvanzado)Ricardos-MacBook-Pro:SistemaDiscusiones ricardoeduardosaucedo$ python manage.py syncdb  --settings=SistemaDiscusiones.settings.local
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/ricardoeduardosaucedo/DjangoAvanzado/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/ricardoeduardosaucedo/DjangoAvanzado/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/Users/ricardoeduardosaucedo/DjangoAvanzado/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/ricardoeduardosaucedo/DjangoAvanzado/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/ricardoeduardosaucedo/DjangoAvanzado/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/ricardoeduardosaucedo/Curso Django Avanzado/SistemaDiscusiones/apps/users/models.py", line 5
    class UserManager(BaseUserManager):
    ^
IndentationError: unexpected indent

Tags: djangoinpyimportexecutemodelslibpackages
2条回答
from django.db import models

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin

    class UserManager(BaseUserManager):

        def _create_user(self, username, email, password, is_staff, is_superuser,
                **extra_fields):

            if not email:
                raise ValueError('El email debe de ser obligatorio')

            email = self.normalize_email(email)
            user = self.model(username=username, email=email, is_activate=True,
                    is_staff=is_staff, is_superuser=is_superuser, **extra_fields)
            user.set_password(password)
            user.save(using = self.db)
            return user

        def create_user(self, username, email, password=None,
                **extra_fields):    
            return self._create_user(username, email, password, False, False, **extra_fields)

        def create_superuser(self, username, email, password,
                **extra_fields):
            return self._create_user(username, email, password, True, True, **extra_fields)

    class User(AbstractBaseUser, PermissionsMixin):

        username = models.CharField(max_length=50, unique=True)
        email = models.EmailField(max_length=50, unique=True)
        first_name = models.CharField(max_length=100)
        last_name = models.CharField(max_length=100)
        avatar = models.URLField()


        objects = UserManager()

        is_activate = models.BooleanField(default=True)
        is_staff = models.BooleanField(default=False)

        USERNAME_FIELD = 'username'
        REQUIRE_FIELDS = ['email']

        def get_short_name(self):
            return self.username

回溯看起来很吓人,但是在这种情况下,如果你从底部开始,然后再往上走,那么信息就非常清晰了。在

  File "/Users/ricardoeduardosaucedo/Curso Django Avanzado/SistemaDiscusiones/apps/users/models.py", line 5
    class UserManager(BaseUserManager):
    ^
IndentationError: unexpected indent

错误消息告诉您在Avanzado/SistemaDiscusiones/apps/users/models.py的第5行有一个缩进错误。在

检查该行开头没有空格,并且没有使用制表符。如果仍然无法解决问题,请编辑问题并从该文件中发布代码。在

相关问题 更多 >

    热门问题