I have a Django project that worked well with Django 1.6, but is giving me a lot of trouble as I try to upgrade to Django 1.7.
In this project is an app, my_app which does not contain any models of its own, but contains a file other_models.py, generated to interact with a legacy database using python manage.py inspectdb. I want to use these models only in specific code paths, when I'm connected to a this secondary database. Again, this worked fine with Django 1.6.
Having upgraded to Django 1.7, I am no longer able to run my test suite using python manage.py test. When I do this, I get a huge number of errors that look like the following:
CommandError: System check identified some issues:
ERRORS:
my_app.AccountAccount.created_by_type: (fields.E304) Reverse accessor for 'AccountAccount.created_by_type' clashes with reverse accessor for 'AccountAccount.deactivated_by_type'.
HINT: Add or change a related_name argument to the definition for 'AccountAccount.created_by_type' or 'AccountAccount.deactivated_by_type'.
...
# Tons more of these
These errors are all complaining about models defined in other_models.py. It appears to me that the changes in app-loading process are causing my problems, but I'm not entirely sure.
I have tried setting up an apps.py in this app with the following code:
from django.apps import AppConfig
class MyAppConfig(AppConfig):
name = 'my_app'
models_module = None
and added default_app_config = 'my_app.apps.MyAppConfig' to my my_app.__init__.py, as per the documentation on configuring applications, but to no avail.
I'm at a total loss for what to do next, and would appreciate any information regarding how to control when and how Django attempts to load models, especially when running python manage.py test.
Related
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
I have a django 1.11 project with some rest_framework related apps. Writing tests for new app, I have suddenly gotten the issue
'RuntimeError: Model class core.myApp.models.query_record
doesn't declare an explicit app_label and isn't in an
application in INSTALLED_APPS'
I do have this listed in installed_apps, and in the end, the reason I have this issue is because I have an __init.py__ file in the top level of the project that loads some config for some related celery tasks.
I'm unaware why I haven't seen this issue in other app tests, as there is nothing particularly special about this app or it's model. But, this is causing all tests to fail for this app.
So, my question is, is there a way I can run these unit tests and ignore the projects top level __init.py__ ? Or maybe I should ask, is there a non-hacky way to do it?
the project level __init.py__:
from __future__ import absolute_import
from .celeryapp import app as celery_app
All other app init.py files are empty.
A problem might be, that .celeryapp is trying to import some models that aren't loaded yet.
You can try to add a AppConfig to core.myApp and load/import your celery app in it's ready() method. See the Django docs for more information Django docs for more information
It turned out, in the end this was simply because of how I was running tests
I was running tests like this
./manage.py test myApp --pattern=*.py
The pure wildcard was causing import issues. I should have done this:
./manage.py test myApp --pattern=prefix_*.py
D'oh.
I'm trying to run a server on my computer in Python/Django. In my installed_apps, I had a program called csvimport. It didn't work, so I had to install django-csvimport and I had to change it to csvimport.app.AppConfig in my installed-apps. However, I still get an importerror message saying "no module named AppConfig". (my version of django is 1.8, and django-csvimport is 2.4, by the way) Is this not the correct way to have csvimport in my installed_apps and my program?
Thanks!
AppConfig is Django's base class for the configuration class of custom apps, not necessarily the name of the config class of the app. But you're almost there:
csvimport.app.CSVImportConf
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.
In my Django project I have a few packages/folders that are not applications themselves, but contain code that uses Django models and needs to be covered with tests.
Obiously, when I run 'test nonapppackage.testname' I get:
django.core.exceptions.ImproperlyConfigured: App with label xyzutils is
missing a models.py module.
What's the best to configure unittest for out-of-app code?