python manage.py makemigrations : No changes detected - python

I am working on a project using django and I am using pycharm software. In my 'store' directory i have a python package called 'models' in which there are two python files :- init.py and product.py . The problem i am getting is that I cannot create table using the command even when I am importing the 'Product' class from product module in init.py file. Even both the modules are also in the same package.
Here is the code of the product.py file:-
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=50)
price = models.IntegerField(default=0)
description = models.CharField(max_length=200, default='')
image = models.ImageField(upload_to='products/')
And here is the code of init.py file:-
from .product import Product
But when i am running the command on terminal i am getting this error:-
(venv) C:\Users\Admin\PycharmProjects\SMart>python manage.py makemigrations
No changes detected

You must put the name of your app <store> into the list of INSTALLED_APPS inside your settings.

I had the same issue with Django 4. To solve it, I created migrations/__init__.py file in every app directory. So just run these command for every app:
mkdir appname/migrations
touch appname/migrations/__init__.py
Then use manage.py:
python3 manage.py makemigrations
python3 manage.py migrate
And it worked.

Related

ModuleNotFoundError : no module found named <module_name>django

I am trying to make a web application using django. so in the website folder, I made another app named U0.
I made sure to add it in the settings section of the website. but when I try to migrate the manage.py file it throws an error saying, No Module found named U0django
In order to create a new app in Django, you have to use the Django builtin command to create the correct structure of an app for you like this:
django-admin startapp u0
btw I believe if you want to create it on your own you have to create a file named apps.py in your app folder and add these lines to that to help Django identify it as an app :
from django.apps import AppConfig
class U0(AppConfig):
name = 'u0'

How to change the name of a third party app on Django?

I am using django-allauth and its account app conflicts with my account app.
I managed to fix this conflict by creating an app label for allauth.account based on this solution
from django.apps import AppConfig
class AllAuthAccountConfig(AppConfig):
name = 'allauth.account'
label = 'allauth_account'
verbose_name = 'aullauth_account'
and then adding it to installed apps
INSTALLED_APPS = (
...,
'apps.allauth.apps.AllAuthAccountConfig',
...,
)
Now, when I try to migrate it gives an error
CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0001_initial, 0002_email_max_length in allauth_account).
To fix them run 'python manage.py makemigrations --merge'
but python manage.py makemigrations --merge also fails with the following error:
ValueError: Could not find common ancestor of ['0001_initial', '0002_email_max_length']
How can I change the allauth.account app name so it doesn't conflict with my app or my migations?
you can use django rename app package package link. I have never use it personally but it seems to be good. you can try it

models.py not getting generated when using the inspectdb

I am trying to generate models.py from a legacy database using the inspectdb command. Everything was fine, i see the table names being inspected and corresonding classes made in the console, after that operation is completed, I checked the directory where the manage.py is residing, and there was no models.py generated.
Can anyone throw some light on this?
Thanks,
John
The inspectdb command does not automatically create a models.py file, by default it outputs the models to the console.
You can save the output to a file on Unix systems with
$ python manage.py inspectdb > models.py
Note this will overwrite models.py if it already exists!

Django 1.7 - makemigrations not detecting changes - managed models

I have just installed django 1.7 in my virtual env.
Then I manually created the following files:
service_bus/
service_bus/__init__.py
service_bus/django_settings.py
service_bus/models
service_bus/models/__init__.py
service_bus/models/dsp.py
service_bus/models/audience_type.py
service_bus/models/category.py
service_bus/models/audience.py
service_bus/models/dsp_config.py
service_bus/models/apsettings.py
So I have a settings file service_bus/django_settings.py and the service_bus app.
Then I did, on bash:
export DJANGO_SETTINGS_MODULE='service_bus.django_settings'
Then I just try to run makemigrations, but it says no changes are detected.
$ django-admin makemigrations
Loading properties from /etc/s1mbi0se/dmp.ini
System check identified some issues:
WARNINGS:
?: (1_6.W001) Some project unittests may not execute as expected.
HINT: Django 1.6 introduced a new default test runner. It looks like this project was generated using Django 1.5 or earlier. You should ensure your tests are all running & behaving as expected. See https://docs.djangoproject.com/en/dev/releases/1.6/#new-test-runner for more information.
No changes detected
$ django-admin makemigrations service_bus
Loading properties from /etc/s1mbi0se/dmp.ini
System check identified some issues:
WARNINGS:
?: (1_6.W001) Some project unittests may not execute as expected.
HINT: Django 1.6 introduced a new default test runner. It looks like this project was generated using Django 1.5 or earlier. You should ensure your tests are all running & behaving as expected. See https://docs.djangoproject.com/en/dev/releases/1.6/#new-test-runner for more information.
No changes detected in app 'service_bus'
In all my models I have something like
class APSettings(models.Model):
...
class Meta:
db_table = u'APSettings'
app_label = 'service_bus'
What could I be missing?
You need to run the migrate command first to scaffold the database schema. Then, you can run makemigrations for each app. Check the Django tutorial for more on this.
Make sure you update your models.py file to actually import the models. For example in models.py you'd have from service_bus.models.audience import *. The manage script goes over that file, imports all the models in audience.py and detects changes in there. If you did not add your new models to models.py then the manage script won't know about the new models in you models files.

Django: Can't run custom commands

I've written a simple custom command, hello.py:
from django.core.management.base import NoArgsCommand
class Command(NoArgsCommand):
help = "prints hello world"
def handle_noargs(self, **options):
print "Hello, World!"
When I run python manage.py hello it returns
Unknown command: 'hello'
I've put it in the management/commands directory beneath my app.
I've added __init__.py files to the management and commands directory.
I've checked my app is in INSTALLED_APPS in settings.py
I've tried installing it in different apps and from the project root too
Running python manage.py syncdb etc is fine. And if I type python at the command line I can import django.core.management ok.
I know I'm missing something obvious, but can't figure out what.
How can I debug this to work out why my custom command won't run?
The problem was that I had another project on my PYTHONPATH. D'oh! I think it was picking up the settings.py from there first so didn't see my app. What pointed me in this direction was I tried running python manage.py create_jobs myapp (from django command extensions) and it returned an error indicating the app couldn't be found. Also #knutin mentioned INSTALLED_APPS.
It is because the __init__.pyc does not get created automatically within "management" and "commands" folder.
Copy your_app/__init__.py and your_app/__init__.pyc and paste it within the management/ and commands/ folder.

Categories