2016-04-08 14 views
5

Chỉ cần tạo Mô hình người dùng trong models.py để lưu người dùng trong cơ sở dữ liệu. Mô hình này trông như thế này:Lỗi khi di chuyển mô hình Người dùng mới

class User(AbstractBaseUser, PermissionsMixin): 
    username = models.CharField(_('username'), max_length=30, unique=True, 
     validators=[ 
     validators.RegexValidator(re.compile('^[\[email protected]+-]+$'), _('Enter a valid username.'), _('invalid')) 
    ]) 
    first_name = models.CharField(_('first name'), max_length=30, blank=True, null=True) 
    last_name = models.CharField(_('last name'), max_length=30, blank=True, null=True) 
    email = models.EmailField(_('email address'), max_length=255) 
    is_staff = models.BooleanField(_('staff status'), default=False,) 
    is_active = models.BooleanField(_('active'), default=False,) 
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now) 

    USERNAME_FIELD = 'username' 
    REQUIRED_FIELDS = ['email',] 

    class Meta: 
     verbose_name = _('user') 
     verbose_name_plural = _('users') 

    def get_full_name(self): 
     full_name = '%s %s' % (self.first_name, self.last_name) 
     return full_name.strip() 

    def get_short_name(self): 
     return self.first_name 

    def email_user(self, subject, message, from_email=None): 
     send_mail(subject, message, from_email, [self.email]) 

Tôi cũng được thêm vào trong settings.py đoạn mã này:

AUTH_USER_MODEL = "myapp.User" 

Tuy nhiên, khi tôi cố gắng makemigrations để áp dụng các thay đổi, nó xuất hiện lỗi này :

Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\core\management\__init__.py", line 353, in execute_from_command_line 
    utility.execute() 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\core\management\__init__.py", line 345, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\core\management\base.py", line 348, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\core\management\base.py", line 399, in execute 
    output = self.handle(*args, **options) 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\core\management\commands\makemigrations.py", line 105, in handle 
    loader.project_state(), 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\db\migrations\loader.py", line 338, in project_state 
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps)) 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\db\migrations\graph.py", line 280, in make_state 
    project_state = self.nodes[node].mutate_state(project_state, preserve=False) 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\db\migrations\migration.py", line 88, in mutate_state 
    operation.state_forwards(self.app_label, new_state) 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\db\migrations\operations\models.py", line 158, in state_forwards 
    apps = state.apps 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\utils\functional.py", line 33, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\db\migrations\state.py", line 162, in apps 
    return StateApps(self.real_apps, self.models) 
    File "C:\Python\lib\site-packages\django-1.9.1-py3.5.egg\django\db\migrations\state.py", line 235, in __init__ 
    raise ValueError(self._pending_models_error(pending_models)) 
ValueError: Unhandled pending operations for models: 
    myapp.user (referred to by fields: admin.LogEntry.user) 

Vì vậy, tôi đang tìm kiếm thông tin về các giải pháp hoặc nguyên nhân có thể có cho lỗi này nhưng không thể hiểu tại sao điều đó xảy ra. Có thể nó phải là một vấn đề với một số di cư trước đó? (Nếu vậy, tôi không có đầu mối nào trong đó cũng không phải lý do). Tôi muốn thêm thông tin nếu cần, nhưng tôi thực sự đánh giá cao bất kỳ đầu mối nào về nguyên nhân của lỗi này.

Trả lời

9

custom user docs cảnh báo cụ thể chống lại việc chuyển đổi Mô hình người dùng sau khi bạn đã tạo di chuyển; có quá nhiều sự phụ thuộc để làm việc đúng cách. Bạn có thể cần xóa db và di chuyển hiện tại của mình và bắt đầu từ đầu.

+0

Tôi thấy, vì vậy tôi có thể sử dụng hệ thống Người dùng mặc định mà tôi có thể thấy trong/admin/trang và sẽ không có vấn đề gì phải không? Tôi sẽ không thể chỉnh sửa hoặc nâng cấp nó (hoặc tạo một UI tương đối phức tạp)? – Jim

Các vấn đề liên quan