0

我有两个 django 应用程序:“home”和“user_profile”。我对“user_profile”应用程序进行了更改,我想为它进行迁移,所以我写了

manage.py makemigrations user_profile

我得到了这个错误:

  File "D:\example\project\home\forms.py", line 31, in <module>
    class AddNewUserProfileForm(forms.ModelForm):
  File "D:\example\lib\site-packages\django\forms\models.py",
line 257, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (birthday) specified for User

好的,我所做的更改与我之前编写的代码冲突,因此引发了此异常。但是,如果我从中删除“家庭”应用程序INSTALLED_APPS- 错误不会消失,即使现在“家庭”根本不应该被视为项目的一部分(如果我理解正确的话)。

所以我有几个问题:

  1. 如何在不重构所有其他代码的情况下为 user_profile 进行迁移?例如,我只想看看它将如何在数据库中呈现,我不关心来自其他应用程序的视图和表单。

  2. 是否有可能在一切完成后进行迁移和迁移并处理此类错误?对我来说,在执行期间处理它们似乎比在 makemigration 阶段更容易处理。

当然,我知道处理它的一种简单快速的方法就是重写所有冲突的东西,但这会花费很多时间,而且我现在并不真正关心大多数东西,我想留到以后。

我使用 Django 1.9.1

user_profile.models

from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.core.validators import RegexValidator
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone

class MyUserManager(BaseUserManager):
    def create_user(self, phone_number, password=None):
        if not phone_number:
            raise ValueError('Users must have a phone number')

        user = self.model(
            phone_number=phone_number,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, phone_number, password):
        user = self.create_user(phone_number,
            password=password,
        )
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user



class User(AbstractBaseUser, PermissionsMixin):
    phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    phone_number = models.CharField(validators=[phone_regex], blank=True, max_length=15, unique=True) # validators should be a list
    is_active = models.BooleanField(
        _('active'),
        default=True,
        help_text=_(
            'Designates whether this user should be treated as active. '
            'Unselect this instead of deleting accounts.'
        ),
    )
    is_staff = models.BooleanField(
        _('staff status'),
        default=False,
        help_text=_('Designates whether the user can log into this admin site.'),
    )
    date_of_birth   = models.DateTimeField(blank=True, null=True)
    date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    email = models.EmailField(_('email address'), blank=True)
    USERNAME_FIELD = 'phone_number'
    REQUIRED_FIELDS = []

    objects = MyUserManager()

    def get_full_name(self):
        fullname = self.first_name+" "+self.last_name
        return self.fullname

    def get_short_name(self):
        return self.first_name

    def age(self):
        import datetime
        return int((datetime.date.today() - self.date_of_birth).days / 365.25  )

    def __unicode__(self):
       return self.first_name + ' ' + self.last_name
4

0 回答 0