Django Error in models.py

时间:2015-09-14 16:00:11

标签: python django python-2.7 django-models

I have a question, when you create class and synchronize database brand me the following error.

(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

2 个答案:

答案 0 :(得分:2)

Tracebacks can look scary, but in this case, the message is quite clear if you start at the bottom and work up.

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

The error message is telling you you have an indentation error on line 5 of Avanzado/SistemaDiscusiones/apps/users/models.py.

Check you do not have any spaces at the beginning of that line, and that you are not using tabs. If you still can't fix the problem, edit your question and post the code from that file.

答案 1 :(得分:0)

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