I have two django apps: 'home' and 'user_profile'. I made changes to 'user_profile' app and I want to makemigrations for it, so I write
manage.py makemigrations user_profile
And I got this error:
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
OK, I made changes that conflict with the code I wrote earlier, thus this exception raises. But, if I remove 'home' app from INSTALLED_APPS
- the error does not go away, even if now 'home' shouldn't be treated as a part of the project at all (if I understand correctly).
So I have couple of questions:
How can I make migration for user_profile without refactoring all other code? E.g. I just want to see how it will be presented in database, I don't care about views and forms from other apps.
Is it possible to makemigrations and migrate and deal with this kind of errors AFTER everything is done? For me it seems to be quite easier to deal with them during execution than in the makemigration stage.
Of course I understand that one easy quick way to deal with it is just to re-write all the conflicting stuff, but that would just take much time and I don't really care about most of the stuff for now, I would like to leave it for later.
I use 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