Django Cannot Import Local Module During Migrations - python

In my project i've got two custom objects, defined in a local fields.py file & blocks.py file. I import them in my models.py as such:
from . import fields as blockfields
from . import blocks
and when running migrations, the autogenerated migrations look like this:
import PROJECTNAME.fields
import blocks
and since blocks isnt in the local scope of the migrations folder, it throws an error. If i manually change it to "import PROJECTNAME.blocks as blocks" then it runs fine, but it'd be silly to have to do that every time. Any thoughts?
Updates:
Using Django 2.1.4, Python 3.6.7
Folder structure is thus:
PROJECTNAME
-fields.py
-models.py
-blocks/
--__init__.py
--*.py
-migrations/
--*.py
Ive variably tried having a blocks.py file which just points to the folder, but it doesn't change the import scope for the automigration.
In my blockfields, i refer to blocks also, which is how the references get baked into the migrations. The fields.py file has an extension of django's generic models.Field, while the blocks are custom types from scratch.

Related

Django, importing models class into new file. Import error attempted relative import with no known parent

I am working on learning Django by making a simple analysis dashboard. I did the startproject command and the startapp command like the tutorial ran through. I added a new file called connectionOracle.py in the app folder.
My folder structure is (top folder is was created via venv)
AnalysisSite
|AnalysisSite
|coreAnalysis
||models.py
||connectionOracle.py
I have a class called WorkOrder in the models.py file. I am trying to import it into the connectionOracle.py file by doing the following
from .models import WorkOrder
I then go to the Command Line (on windows) and navigate tot he coreAnalysis folder and run the following
python connectionOracle.py
I get an error that says.
ImportError: attempted relative import with no known parent package
I did some reading online, and I tried doing an absolute path with AnalysisSite.AnalysisSite.coreAnalysis.models
that didnt work. I also tried moving the connection file to different directories and that didnt work either. I also tried going into the command line and typing set DJANGO_SETTINGS_MODULE = AnalysisSite.settings
I also put a _init_.py in each folder. (Django automatically put it into the project directory and app directory).
I am not sure what I am doing wrong
you are trying to access Django components(models) by a script file ,
the caller in this case not Django itself ( the request not coming from url or different django tools or mechanism),
anyway in your custom python file which is 'connectionOracle.py'
try to do some steps before accessing the models itself,
the steps are available on the following URL:
https://stackoverflow.com/a/68936419/12662056
############
change the path for project depending on your project path.
i hope this helpful

how to import own modules in Python?

I have a main.py file and I need to import the database tables that are in "entity / models.py". How do I do ?
I use linux and I don't want to add models.py to sys.path, I want models.py to be visible only in this project. I also don't want to create symbolic links because in my opinion it's the same thing as putting models.py inside the app itself.
You can convert the directory into a package with a __init__.py file. Then as usual you can import the module from the package as in:
from entity.models import func
The __init__.py file can just be an empty file. Just its existence will treat the directory as a package.
Hope this answer helps.

Django: no such table snippets_snippet

I'm following this Django Rest tutorial on serialization: http://www.django-rest-framework.org/tutorial/1-serialization/#getting-started
I followed it pretty much to the letter. It gives the above error when I try to save a snippet.
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
snippet = Snippet(code='foo = "bar"\n')
snippet.save()
I'm working on Windows. The tutorial is made for Apple. I have had to enter some commands slightly differently for this reason. I have no idea if this has to do with what is wrong in this case.
I don't know where to even start in figuring out the problem here, so I could use any help. Thanks.
You need to make the migration (ie: forcing the db to corespond to what is defined by your new python code)
Do the following:
python manage.py makemigrations snippets
python manage.py migrate
You should remove db.sqlite3 as well as snippets/migrations. Later, create a folder named migrations inside snippets folder and create a __init__.py file inside snippets/migrations/ folder. Finally run:
python manage.py makemigrations && python manage.py migrate
Be sure you are into the virtual environment with the correct python version.
I believe the error refers to the models.py. Could you show the file so I can double check that too. Also there is a chance for unapllied migartions, double check if you've done this as well.

Can a Python script use a Django database while not being in the same folder?

I have a python application (which we'll call app) I'd like to have a web front for. The application has many files and it's important the folder tree structure stays the way it is.
I've built a Django project and put it in a folder named "Web" in the app's folder, so now the folder tree looks like so:
[Data]
[Resources]
[Web]
[WebFront]
normal django app files
[Web]
__init__.py
settings.py
urls.py
wsgi.py
__init__.py
manage.py
main.py
Here's the code on the app's main.py:
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web.Web.settings")
django.setup()
This code causes an exception on the django.setup() line as (I think) django does not find the project modules: ImportError: No module named WebFront (WebFront is the name of the django app)
I suspect this is caused because django runs in the directory of python app, and therefore cannot find the folder WebFront - Which should actually be Web/WebFront
Can this be done? Or should I reverse the order and put the python app in the django app?
This is not a duplicate of the following questions as the folder nesting causes a different problem (I think)
Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
Easiest way to write a Python program with access to Django database functionality
Using only the DB part of Django
You can locate your main.py script where you like. However, if it is outside of the Web folder, then you will have to add Web to the Python path, otherwise imports like import Webfront are going to fail.
import sys
sys.path.append('/path/to/Web/')
Once you have done that, you can change the DJANGO_SETTINGS_MODULE to
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web.settings")

Install Django Application so It's Available on Python Path

I have a django application structured like this...
app_foo
__init__.py
urls.py
views.py
models.py
bar_app
__init__.py
...
bar_app...
By using distutils, I can get the application to install into the python path under the "app_foo" module name.
However, any of the code inside of the "bar_app" python files which refers to things inside the django app relatively does not work when executed from the python path. For example,
from bar_app.views import stuff
I know that I can go through the app and change all the references to be absolute. For example,
from app_foo.bar_app.views import stuff
My question:
Is there anyway I can get all of the apps inside "app_foo" to also be on the python path?
Conceptually this would be similar to saying from app_foo import * for the entire path.
You can do
from .bar_app.views import stuff
http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports

Categories