0

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:

  1. 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.

  2. 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
ScienceSamovar
  • 422
  • 7
  • 15
  • 1
    Could you please show us your relevant models code? – Wtower Feb 22 '16 at 09:24
  • @Wtower, added it to the question. Though I think it is not relevant, since exception raises in different app. The problem is that it checks 'home' app during 'makemigration' for some reason, even if 'home' is not in INSTALLED_APPS – ScienceSamovar Feb 22 '16 at 10:00
  • Ok, where is this field `birthday` supposed to be, which raises the error? Is it defined in your `ModelForm`? It surely is not in your models. – Wtower Feb 23 '16 at 10:36
  • @Wtower, I was using `birthday` and switched to `date_of_birth` later, but ModelForm in **home app** still has `birthday` there. The problem is - I make migration only for `user_profile` and I even deleted `home` from 'INSTALLED_APPS', so I wonder why `makemigrations` command raises error when all it should do is to make migration for `user_profile` – ScienceSamovar Feb 23 '16 at 12:25
  • This is very difficult to tell with having access to the entirety of your code. It could be any import somewhere forgotten. In any way, I still do not understand why not fix the error in ModelForm. Is this supposed to be simply a theoretical question? – Wtower Feb 23 '16 at 12:31
  • @Wtower, well, yes, I can fix it(already did actually), but I don't understand why the exception is raised anyway. So yeah, it is more theoretical, but it also would be nice to see practical way to deal with it other than re-write the whole code, e.g. in my example I just did couple of changes and wanted to look at database structure I get, i.e. it is not related to the `home` app at all. Even if it raises some errors - it would be logical to raise errors when I will work with `home` app. – ScienceSamovar Feb 23 '16 at 13:01

0 Answers0