Split Models over Packages - django

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.

Related

AppConfig override restricts first makemigration

I am overriding the AppConfig and adding below in __init__
default_app_config = 'api.apps.AppnameConfig'
which has some models check I want to create them if doesnot exits all works fine. But when I am deploying this to another machine
python manage.py makemigrations
fails obviously because there are no tables created as there is no migration on fresh project. It is raising ProgrammingError I can do try pass on this but I dont want to go this way.
I also checked if migrations folder exists works fine but again fails on migrate.
Please suggest best way to do this.
Solved it by checking the tables introspection returns [] if no migration is ever applied in __init__
from django.db import connection
if connection.introspection.table_names():
default_app_config = 'pathto.AppnameConfig'

ValueError in Django when running the "python manage.py migrate" command

I needed to add more fields to Django's User model, so I created a custom model class (named Accounts in an app named accounts) that extends Django's AbstractUser class.
After that, I updated my settings.py file, defining the AUTH_USER_MODEL property:
AUTH_USER_MODEL = 'accounts.Accounts'
I then created a migration file for the custom model using the python manage.py makemigrations command.
After that, I ran the python manage.py migrate command and I got this error message:
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.accounts', but app 'accounts' isn't installed.
What's the cause of the error and how can I fix it?
UPDATE:
Now, if i run the python manage.py makemigrations command, I get this error message:
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'accounts.accounts', but app 'accounts' doesn't provide model 'accounts'.
You just delete your previous 0001_initial.py in the migrations folder and try doing the makemigration and migrate again
I have the similar problem. It is the admin app has the cache and migrations history. I solve it by deleting all the cache and migrations history record(pycache file, and 0001.intial etc., keep init.py only) in
YouProject\lib\site-packages\django\contrib\admin\migrations
I too had a similar problem when I changed the name of one of my apps, I had to delete migrations files at two locations, all migrations for the specific app migration folder, then migrations at "Your-project-env/lib/python3.5/site-packages/django/contrib/admin/migrations".
You didn't add accounts to your INSTALLED_APPS. From the comment, I can see accounts.apps.AccountsConfig in your list of apps. Instead of it, just add accounts to your INSTALLED_APPS
It's just because you have already an instance of default user model I think. Start a new project and migrate your models again and it should work.
I have the same problem.
look under your class in models.py, change the app_label to the name of your application, I put the name of the class, and then the error came. doing this I think it fixes the errors.
class Meta:
app_label = 'put_app_name'
Sorry for some grammar error, I'm using google translator.

Unable to import models in __init__.py django 1.9.4

My directory structure is.
Mypack:
--> __init__.py
--> admin.py
--> apps.py
--> foo.py
--> models.py
In apps.py, I have AppConfig. I have some methods in foo.py which uses the models imported from models.py. and I imported all methods in init.py.
from foo import *
I make this app installable with pip. when I install it in other django app and try to run python manage.py check. it gives the following error.
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
please suggest me, how to fix this issue?
From the 1.9 release notes (emphasis mine):
All models need to be defined inside an installed application or
declare an explicit app_label. Furthermore, it isn’t possible to
import them before their application is loaded. In particular, it
isn’t possible to import models inside the root package of an
application.
You are (indirectly) importing your models into the root package of your app, Mypack/__init__.py. If you still want to import the functions in foo.py into your root package, you need to make sure that it doesn't import the models when the module is first imported. One option is to use inline imports:
def foo():
from .models import MyModel
MyModel.objects.do_something()
If importing the functions in your root package is not a hard requirement (e.g. for backwards compatibility), I would personally just stop importing them in __init__.py and change other imports to use Mypack.foo.

Migrate models in package hierachy without South

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.

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.