ILIKETOLEARN
ILIKETOLEARN

Reputation: 55

Django makemigrations doesn't create migrations for my app

Here’s a brief overview of what I’ve accomplished so far, to ensure we're all on the same page. Please note that I’m still new to Django, so my approach might not be perfect or even close to what it should be.

Created a new Django project and applied the initial migrations right away (18 or 19)

Next thing I did is create a new app called "authorization", so like this

python3 manage.py startapp authorization

And then I added it to the "INSTALLED_APPS" list in my projects settings.py file

I then created the following in the models.py file

from django.db import models
from django.core.validators import MinValueValidator
from django.utils import timezone

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin

class CustomUserManager(BaseUserManager):
    def create_user(self, email, password, **extra_fields):
        if not email:
            raise ValueError('Please provide a value for email!')
        email = self.normalize_email(email)
        user = self.model(email = email, **extra_fields)
        user.set_password(password)
        user.save(using = self.db)
        return user
        
    def create_superuser(self, email, password, **extra_fields):
        extra_fields.setdefault('role', 'OWNER')
        extra_fields.setdefault('subscribed', True)
        return self.create_user(email, password, **extra_fields)
    
class User(AbstractBaseUser, PermissionsMixin):
    # id provided by default
    username = models.CharField(
        max_length = 30, 
        unique = True
    )
    email = models.EmailField(unique = True)
    # password provided by default
    favoriteFood = models.CharField(max_length = 255)
    favoriteSnack = models.CharField(max_length = 255)
    favoriteDrink = models.CharField(max_length = 255)
    # Define the roles
    ROLE_CHOICES = [
        ('USER', 'User'),
        ('CREATOR', 'Creator'),
        ('ADMIN', 'Admin'),
        ('OWNER', 'Owner'),
    ]
    role = models.CharField(
        max_length = 10, 
        choices = ROLE_CHOICES, 
        default = 'USER'
    )
    createdAt = models.DateTimeField(auto_now_add = True)
    updatedAt = models.DateTimeField(auto_now = True)
    deletedAt = models.BooleanField(default = True)

    def soft_delete(self):
        self.deletedAt = timezone.now()

    objects = CustomUserManager()
 
    USERNAME_FIELD = 'email'

    REQUIRED_FIELDS = ['username']

    def __str__(self):
        return self.email

Then I created a key called "AUTH_USER_MODEL" and set it to "authorization.User".

But then when I do

python3 manage.py makemigrations

I get returned to me

No changes detected

Why aren't my changes being recognized?

Upvotes: 0

Views: 59

Answers (1)

Vegard
Vegard

Reputation: 4882

Sometimes Django doesn't "recognize" new apps when nothing has been initialized yet. I couldn't tell you exactly why, but I notice the same in my own projects, possibly it's something to do with the migrations folder and whether its __init__.py exists ... but that's pure speculation on my end.

At any rate, this will fix it:

python manage.py makemigrations <app_name>

That'll instruct Django to create migrations for that specific app. Once you've done that, subsequent changes will get detected by makemigrations as usual.

Upvotes: 0

Related Questions