import error when running django south - django

I added a ManyToMany field between my facebook user and car Reviews to ./facebook/model.py which required me to insert:
from car.models import Review
I try to run:
./manage.py schemamigration facebook --auto
but i get error:
django.core.exceptions.ImproperlyConfigured: ImportError haystack: cannot import name Review
The problem is, my facebook app has nothing to do with the third party haystack module. I tried some simple debugging and found as long as i try to import Review, i get the error. It doesn't matter if I change the model or not. Could it be the order of my "INSTALLED_APPS"? I have "car" followed by "facebook" and then "haystack".

So, a bit of background on how imports work: When you run a statement like "from x.y import z", the entire module x.y is executed, and then the interpreter pulls z and places it in your namespace.
So, your underlying problem probably has nothing to do with South or Haystack; it's probably in car.models somewhere. That's why you're getting an error no matter how you come across the importing of Review, and you'll likely get it if you try to import anything else from that module.
Check car.models for problems -- in particular, you might have a circular import (in other words, a case where A imports from B and B imports from A).

Or simply the model with its files does not exist in your environment but does exist in your configuration :)
(happened to me when I forgot to "git add" a folder and after building into staging got this error...)

Related

Test failure because model couldn't be imported

Application I am working on is proprietary and thus I will try to provide as much information as possible.
When running python manage.py test, which runs all the tests, only one application among many others fails. Too many hours have been burned on this.
The output is:
ImportError: Failed to import test module: app.aom.apps.forum.tests
after this, tracing is listed and then one line which says that the problem occurs when importing models into tests.py file, that is:
from .models import ForumSectionGroup, ForumSection, ForumThread, ForumPost
and the last line of the output is:
RuntimeError: Model class app.aom.apps.forum.models.ForumSectionGroup doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.
I have Googled and researched what could cause this problem, and the conclusion: either I am importing module before application is loaded or I don't have the application listed in INSTALLED_APPS. But none of these seems to be the problem. Maybe testing mechanism somehow skips few steps and renders the model unloaded before importing it.
Explicitly assigning app_label as part of class Meta in the model results in conflict, because the model ends up registered twice, when I force it. I was driven to this conclusion by looking at the code at line 111, https://github.com/django/django/blob/master/django/db/models/base.py
I ran into this same issue. For me what fixed it was changing
from .models import Model1, Model2
to
from app.models import Model1, Model2
The from .model import syntax works fine in view.py, etc. but in the tests it was not working. This only seems to be the case when using a non-standard structure as pointed out in a comment above.
In my specific case I was using Django 1.11.

Moving from Django 1.6.x to 1.9.x import model errors

I have a few defined apps in my Django project, each with their own sub-directory (created with startapp)
In the views.py of app1 I have an import to a model from app2
from app2.models import MyModel
This worked in Django 1.6.x. In version 1.9 I get:
Could not resolve variable
sometimes on MyModel, sometimes on the filter(..) method, or on both.
If I change the import to
from app2.models import * ##UnusedWildImport
then everything works just fine.
Has anything changed in 1.9.x (or before) that requires a different mode of importing models external to the app?
I think I can rule our circular import problems as this would have failed in 1.6...
Edit: Based on the comments I started wondering whether this might be a PyDev problem.
I tried:
Removing and re-adding Python to PyDev - it did not help
This https://stackoverflow.com/a/8534599/5958359 - removing the myproject/src folder from PYTHONPATH worked ... with a caveat.
The error did not appear when I completely removed the import statement, so this is not a good solution
This is a PyDev error.
Searches haven't yielded an adequate solution - most simply explain how to disable the error - so I will not link to any solution here.
My workaround, as much as I don't like from xxx import * seems like the best temporary solution.

Django import models

OK -- I've been working with Django for a few months now and have come across a weird issue. To set it up, here's my webapp structure.
The main Django project is called cpm.
I have a bunch of django apps in the cpm folder. Within each app, I have my models.py file.
Now, when I want to create/use models from other apps I would do something like:
from cpm.products.models import *
assuming an app named products was present. Recently, I started to get some errors saying things like, cannot import XYZ from products. So, after much searching, I changed the line:
from cpm.products.models import *
to
from products.models import *
I just dropped the cpm. part and now it works.
Can someone tell me why this is happening? It seems to be happening on only portions of my apps (I have a bunch within the CPM project). I want to make sure my syntax is accurate as I move forward.
Thanks!
The project root's directory got removed from the python path somewhere along the way, or you removed the __init__.py file from it's root.
On a side note, importing * will lead to issues, especially when you start adding lots of apps. Consider doing from products import models as prod_models. Then doing prod_models.MyModel where you need to reference your models.

Django: import error that refer to template tags

I'm having a headache with some errors that appear suddenly on an application I'm developing. One time I solved it using complete imports (including the project dir) but this time the error has no sense.
TemplateSyntaxError at /accounts/login/
Caught ViewDoesNotExist while rendering: Could not import e_cidadania.apps.proposals.views. Error was: cannot import name User
And marked code is:
{% trans "Lost your password?" %}
The import line at views.py:24
from django.contrib.auth.models import User
I must say, 24h before everything was working fine and no changes were made to the repo.
I've looked the url and the view, both are fine. I've run manage.py shell and tested the import, works fine. I did put some markers in the code to test how it was running and the program crashes exactly importing the User model in that file (there are lots of imports User in the application and not one of them gave a warning). Even deleting the import from thefile gives the same error!
How can I track this to know what is the real problem?
UPDATE: I forgot to mention that the marked error is in the userprofile module, and the proper error is given in the proposals module, a module that has absolutely nothing to do with userprofile.
UPDATE 2: You can see the code here, the application is GPL so there is no problem.
The "relation" between userprofile and proposals modules is that you probably refer to both in the urls definition, which is used to do the reverse when using {% url 'whatever.viewname' [args] %} there probably you do import some view function from both.
I would start having a look at modules you're importing there.
I had this annoying problem (more than once) and most of the time it was caused by recursive imports or by a order dependent imports that I was not aware of (because most of the times "was working").

Debugging cryptic "Error: cannot import name <Name>" on Django

Sometimes when I run manage.py I get a cryptic message in red that says Error: cannot import name <Name> and nothing else.
Obviously, this is a simple import or syntax error and with a little looking around, I can usually fix it. Other times however it takes me quite a while to figure out what exactly I did wrong. Is there a way to get Django to spit out more info in this situation?
This is an annoying problem. Luckily, it's been fixed (recently): see this ticket and this patch.
If you want to just hack your local django install (you're running under virtualenv or buildout, right?), change to the root of your django directory (the one with README, INSTALL, etc) and run this:
curl "https://code.djangoproject.com/changeset/17197?format=diff&new=17197" | patch -p3\
So, if you run django trunk > 17197, apply the patch to your django install (it applied to django 1.2 for me), or wait until django 1.4, you'll be able to do this:
./manage.py shell --traceback
And you'll get a full traceback instead of just the Error: cannot import ...
Voila!
this happens when a circular import appears, when one model is dependent on another and in turn they try and import each other while executing code.
You might want to paste the two models that are causing you issues.
That'll help us debug.
Also it tends to happen sometimes with signals so if you have a signals file please paste too.