Migrate models in package hierachy without South - django

I'm on Django 1.8. Rather than all my models being in a single models.py file, I have them in a package that looks like this:
models/
- __init__.py
- Album.py
- Artist.py
But Django's makemigrations command appears to work only when an app has a single models.py file. Running django-admin help makemigrations does not bring up any hints on additional options that I can pass to the command to achieve what I want. Also, all other questions and answers I've seen here on SO involve South, which is incompatible with Django 1.8.
So what's the workaround?

Horizontally splitting django models across multiple files is a valid approach. A good explanation can be found here.
Django looks for your models in the models module which can be a file or a valid python package.
If you have a valid python package( a directory with a __init__.py file) it will try to discover the models based on the contents of __init__.py.
Make sure that your __init__.py file looks like this:
from Album import Album
from Artist import Artist
Then django should discover your models correctly.

In your models.py
add.
# myproject/app/models.py:
from models/Album.py import *
from models/Artist.py import *
Note about django-south: From django-1.7 migrations has been started supported. Please check Migrations.

Related

I have a problem with the modules Bitnami Odoo 12 Google

I have a problem with the modules in Bitnami Odoo 12 for Google Cloud.
I am studying the fundamentals of Odoo, and I was able to create my first module, all right, I appeared in the list of modules and installed it, now I am in lesson 2 where I create basic models for this module, I did everything with care, even following the steps odoo documentation, but update, restart the server and the VM several times, but in the list of models does not appear what I created.
What is happening?
Odoo installed modules get's imported, as other's already mentioned in the comment. So if you don't import your models definition. For example, let's say your model definition .py files are organized as following:
root_addon_directory/
__init__.py
models/
__init__.py
my_model.py
Then you can import your model definition using relative path import as following:
root_addon_directory __init__.py file content
from . import models
models directory __init__.py file content
from . import my_model

How to keep out utility functions in a separate directory inside an app in Django

In my current project, I have moved out most of the business logic from my views and have put them under the following directory structure myapp/utility/utility.py. Under the Utility directory i have put the init.py file also. I am using most of the models from my Models.py file in the utils.py file. Now the problem I am facing is when I run python manage.py runserver, it throws up an error saying models not found.
I had also followed the steps listed in the Django docs here. The project structure can be seen below. The following are just below my app directory.
In the utils.py I am trying to do the import
from my_project.myapp.models import Location, Product
This where it is not working.
In Django you may import from project's root directory directly, so in your case it should be something like this:
from myapp.models import Location, Product

Heroku application structure for Django

I started a Django 1.6 application locally, with the structure that it came with:
project
django_folder
wsgi.py
my_app
views.py
models.py
...
my_second_app
views.py
models.py
...
Now when I try and deploy on Heroku, it only seems to work with the Procfile in the top directory. I seem to be able to move manage.py around without problems.
My problem is that I have environmental variables set so that in my_app views.py I do the following:
from models import my_model
When I put it on Heroku, it seems that I need to specify my_app
from my_app.models import my_model
Is it possible to set an environmental variable in Heroku so I don't need the myapp in front of models? (My second app is not finished, but partly integrated. It is going to be a pain to change all the imports in both apps). When I add my_app in settings.py is this supposed to affect import paths?
You don't need environment variables, just use relative imports. So in your views.py:
from .models import MyModel
Note the leading dot making this a relative import. In this case there's no need to do anything more. This is the standard pythonic way of doing things.

PyCharm cannot resolve reference in __init__.py with Django project apps

I am at my wits end with this issue, and would love some help resolving this.
I have a Django project with a bunch of sub apps as such:
my_project/
manage.py
my_project/
settings.py
urls.py
wsgi.py
app_root/
__init__.py
app1/
__init__.py
models.py
views.py
urls.py
templates/
[various templates].html
app2/
__init__.py
models.py
[etc]
app3/
[etc]
in my django settings.py i have installed apps as such:
app_root.app1,
app_root.app2,
In PyCharm, I've tried various things but essentially have Content Root as the top "my_project/" and app_root, app1, app2, etc as Source Roots. I've tried just having app_root as the only Source Root, and I've tried having only app1, app2, etc only as Source Roots, but nothing makes any difference.
Everything functions fine. app runs and everything. However, PyCharm has an inability to resolve my apps.
However, if i try this:
import app_root
...
def some_function(self):
app_root.app1.models.My_Model.objects.all()
it will highlight app1 with the error "Cannot find reference 'app1' in '__init__.py'"
This also means it can't do autocomplete anywhere in the path while doing app_root.app1. - it has no idea about models, views, etc. despite having an (empty) __init__.py in every directory.
I also cannot use any refactoring because it always says "Function is not under the source root"
I've spent countless hours trying to get PyCharm to behave but simply cannot find a way to do it. Is there any way this can be done so PyCharm will autocomplete my apps and not keep giving inspection warnings?
I had some similar issues. My solution; within the PyCharm preferences I added a path to app_root in my active Python Interpreter.
After an exchange with the PyCharm folks, here is what I learned:
Django imports all apps in INSTALLED_APPS variable and their models using __import__ for its own purposes.
In your case, it runs
__import__("app_root.app1")
__import__("app_root.app1.models")
After that, you call import app_root and obtain module app_root with app_root.app1 and app_root.app1.models already imported by internal Django code
Fact that Django imports apps and models is Django internals, it is undocumented and may be changed in future releases. We believe you should not rely on it in your production code, nor PyCharm should.
Here is example in bare python (no django):
__import__("encodings.ascii")
import encodings
print (encodings.ascii.Codec) # this code works, but PyCharm marks "ascii" as "unknown module"
So basically, it's not supposed to work as import app_root, but Django funkiness is masking that.

Split Models over Packages

Isn't it possible to have a package:
models
and then inside the model another package
models.country
and then syncdb it?
I always get an error, even when I import it in __init__.py inside the models package.
What you're doing is fine, but make sure you're setting the app_label property on the models' Meta classes.