Can't run makemigrations after moving modules - django

I've got a module named: validators.py located at ./project/app1/validators.py.
To use it, I have imported it inside one of my Models:
# app1/models.py
from .validators import validate_something
After a while, I decide to move this validators.py to a separated directory utils/ (which contains a __init__.py file), so I can use it in all my apps without referencing the app1.
Now it's located at: ./project/utils/validators.py.
After moving the module, I changed my import at the beginning of the Model:
# app1/models.py
from utils.validators import validate_something
Now that I want to create new migrations using manage.py makemigrations I'm getting this error:
File "project/app1/migrations/0001_initial.py", line 5, in <module>
import app1.validators
ModuleNotFoundError: No module named 'app1.validators'
I can remove all migrations and regenerate them but I guess there should be another way to resolve this dependency issue.

Related

How to import model from one app to another?

The issue I face:
Error: ModuleNotFoundError: No module named 'CVBuilderApp.cvs'
What I did: In my main app views file i.e, in the CVBuilderApp.views file
views.py: from CVBuilderApp.cvs.models import PersonalInfo
My project structure:
CVBuilderApp
- accounts
- models, urls, views
- cvs
- models, urls, views
- CVBuilderApp
- settings.py
- manage.py
How do I import the model while the main application name and the project name are the same? Please help
from csv.models import PersonalInfo
Imports are relative.
You can calling python manage.py runserver so everything is relative to manage.py
Example
CVBuilderApp (root)
- helpers (dir)
- helperofhelper (dir)
- doesthing.py <- Import this
- cvsHelper.py <- from Here
- manage.py
if wanting to import doesthing inside of cvsHelper
you would not do from helperofhelper.doesthing import dothing
you would do from helpers.helperofhelper.doesthing import dothing
edit: missed the from
you should not write project name in imports, so your imports must be something like this:
from cvs.models import PersonalInfo
Model location is relative to the location of manage.py file.
so you need to enter:
from cvs.models import PersonalInfo

Initialising flask environment variable issues

I am trying to create a an application using Flask. I have done this before successfully, however, I am not sure why this is not working this time. Everything seems to be in the right order. I have searched for answers, however, I still can't determine what's going wrong as everything seems logical to me? Yet, it is still going wrong?
ZXM934/
app/
__innit__.py
views.py
venv/
run.py
The following are each files contents:
run.py
# Importing app object which was created in __innit__.py file into app.py
from app import app
if __name__ == "__main__":
app.run()
__innit__.py
# This class will ultimately bring our entire application together.
from flask import Flask
# Creating Flask app.
app = Flask(__name__)
# Importing views file to avoid circular import.
from app import views
view.py
# This class represents the UI of our website.
# Importing app directory. As __innit__.py file is apart of this directory,
# this import treats it as a package.
from app import app
#app.route("/")
def public_home():
return "Homepage"
#app.route("/login")
def login():
return "<h1 style='color: red'>Login</h1>"
I set the environment variables as following within the console:
export FLASK_APP=run.py
export FLASK_ENV=development
I then run the following command:
flask run
The following error occurs:
flask.cli.NoAppException: While importing "run", an ImportError was raised:
Traceback (most recent call last):
File "/Users/zahidmalik-ramzan/Desktop/zxm934/venv/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/Users/zahidmalik-ramzan/Desktop/zxm934/run.py", line 2, in <module>
from app import app
ImportError: cannot import name 'app' from 'app' (unknown location)
I don't understand what I am doing wrong?
Your problem is in the file name __innit__.py.
For python to understand that a folder is an actual package within your project structure you need a special file inside, i.e. __init.py__ ps: no double n.
A regular package is typically implemented as a directory containing an __init__.py file. When a regular package is imported, this __init__.py file is implicitly executed, and the objects it defines are bound to names in the package’s namespace.
Conclusion: change your __innit.py__ to __init.py__

Django: how to set the path for the environ variable "DJANGO_SETTINGS_MODULE"

In Django, I used to write populating scripts and put them in the project root directory. For example,
mysite/
mysite/
manage.py
populateA.py
The first few lines of populateA.py:
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
import django
django.setup()
...
As there are more and more populating scripts, I would like to move them to another package populate:
mysite/
mysite/
manage.py
populate/
__init__.py
populateA.py
populateB.py
populateC.py
...
However, when I run the populating scripts (python populateA.py), I got the error message: ImportError: No module named 'mysite'. How to properly set the path for DJANGO_SETTINGS_MODULE?
Since populate is already a module, run its submodules.
python -m populate.populateA

No fixture named 'X' found

I'm using Django 1.6 and I use South to handle migrations.
In most of my application I used to have initial_data.json files. I converted them to be loaded with migrations rather than automatically by Django (as this is recommended in the documentation)
I was using version 0.8.2 of South when I ran into a weird behavior / bug where loading fixtures is done according to the model code and not the state of the migration. I saw that the newest version (0.8.4) has since added some bug fixes related to loaddata, so I upgraded to it.
Now I get the following error on all the migration that load fixtures:
UserWarning: No fixture named 'X' found.
When I use Django's loaddata it works fine. Any ideas on how to solve this?
South and fixtures
South simply patches syncdb to skip the fixture loading for models with migrations, and actually loads them after the final migration for an app has been run.
Make sure your initial_data file is located in the correct place
Loading initial_data does not require you do actually do something, but place the fixtures in the correct place as explained in Django's documentation.
To quote the docs:
By default, Django looks in the fixtures directory inside each app for
fixtures. You can set the FIXTURE_DIRS setting to a list of additional
directories where Django should look.
This means that if you have an app called "myapp", you'd create a "fixtures" dir inside it and place the json there, e.g.: myproject/myapp/fixtures.
Django 1.7 and newer
Django 1.7 introduced built-in migrations. These have an interface similar to South; management commands to create migrations makemigrations, run them migrate, and others.
However, initial_data fixtures are no longer auto-loaded on syncdb run; unless it is an existing app, and has no migrations. This is mentioned in the release notes.
The docs now recomend to create a datamigration to handle fixture loading. Luckily, this is pretty easy to do, here's how I usually do it:
1. create an empty data migration
$ python manage.py makemigrations --empty myapp
If you had only the initial migration, you end up with these files (note that I renamed migration 0002 for clarity):
myapp/
├── __init__.py
├── fixtures
│   └── my_initial_data.json
└── migrations
   ├── 0001_initial.py
   ├── 0002_load_fixture.py
└── __init__.py
2. update the code of 0002_load_fixture.py to run loaddata
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
from django.core.management import call_command
def load_my_initial_data(apps, schema_editor):
call_command("loaddata", "my_initial_data.json")
class Migration(migrations.Migration):
dependencies = [
('myapp', '0001_initial'),
]
operations = [
migrations.RunPython(load_my_initial_data),
]
This error also occurs when the filename does not include the '.json' extension.
In order to load a fixture from current working directory it has to end with '.json'.
Why don't you run loaddata comand from your migration then?
import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
class Migration(DataMigration):
def forwards(self, orm):
from django.core.management import call_command
call_command("loaddata", "my_fixture.json")
If your still getting the error:
No fixture named 'X' found
Try using the path to the json file, as manage.py is probably not being run from the project directory (it may even be run from the root directory). Try something like this:
import os
app_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__))
json_path = os.path.join(app_path, 'fixtures', 'my_fixture.json')
call_command("loaddata", json_path)

Django ImportError for models.py

(Updating my question with more information.)
My django app is running fine on my dev server.
I have a view that pulls from the database using the below line that works fine:
from myapp.models import MyTable
However, if I add the above 'from/import' to another module (see below structure, it's the module named 'problem_module.py') I'm writing where I want to pull from the sqlite3 database, I get this error.
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'myfolder.settings' (Is it on sys.path?): No module named myfolder.settings
I've read and tried various solutions recommended when people get this error, but I missing something because i'm unable to resolve it.
I'm using Django 1.4 and have the lay-out as recommended.
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
wsgi.py
myapp/
__init__.py
models.py
admin.py
views.py
indevelopment/
__init__.py
problem_module.py
I figured out what was happening and why after going through the traceback carefully and looking at the django source code. Here is what happens.
When you run:
python manage.py runserver
the environment variable gets set properly assuming you already changed this small little file or just don't pay attention to it because django 1.4 automatically configures it for you.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
However, because this setting of os.environ is under a:
if __name__ = "__main__"
expression, it only gets run if call that file directly, as you do with:
python manage.py runserver
Otherwise, if you are running a file that needs that environment variable - say testing a module in Eclipse - , the os.environ needs to get set in another place (shell, etc).
All the that I got generally pointed to this but I needed the context.
But as a little adjustment (yes, not a good idea as it couples) on the source code you can also hardcode it in manually in/django/conf/__init__.py
Specifically to see where it happens, the change below works:
# in module: /django/conf/__init__.py
class LazySettings(LazyObject):
def _setup(self):
try:
# Comment out the call to os.environ and hardcode in your app settings
# settings_module = os.environ[ENVIRONMENT_VARIABLE]
# WARNING: bad practice to do this. ;.
settings_module = "myapp.settings"
Have you changed/set DJANGO_SETTINGS_MODULE?
Try export DJANGO_SETTINGS_MODULE=mysite.settings and start your dev server.
modify your manage.py:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
PyCharm sometimes override DJANGO_SETTINGS_MODULE to empty string. Try to debug your manage.py and see if it realy changes after setdefault() call.
If its not either change pycharm settings or use os.environ['DJANGO....']='my_settings'..
or hack files at .idea/. .idea/workspaed.xml contains
env name="DJANGO_SETTINGS_MODULE" value="" in this case