django refactoring models - django

I've been doing a little clean up on my django project and so I decided to rename some models remove some unnecessary fields etc. I dropped all the tables from the dbase and reran "syncdb". However, now I'm getting and error
Could not import pollsite.polls.views. Error was: cannot import name OldTableName
Its a template error from base.html
OldTableName doesn't exist anymore (I've renamed it). Is there something else I need to run to get the admin site to work properly with the new schema?

You need to check your imports in your pollsite.polls.views. Most likely you still have an import of the old model name in that file.

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.

django-selectable LookupAlreadyRegistered error

i am trying django-selectable the 'fruit' example given in the docs here -
https://django-selectable.readthedocs.org/en/version-0.6.2/quick-start.html#defining-a-lookup
getting LookupAlreadyRegistered error,there is a small note on this error,but i can't
figure out how to solve this.i understand that it has something to do with import statements, i am using django-1.3
i will provide more info if needed , but i am just using the same code as given in the example.
kindly help
Check the note in the link you've posted:
You should only register your lookup once. Attempting to register the same lookup class more than once will lead to LookupAlreadyRegistered errors. A common problem related to the LookupAlreadyRegistered error is related to inconsistant import paths in your project. Prior to Django 1.4 the default manage.py allows for importing both with and without the project name (i.e. from myproject.myapp import lookups or from myapp import lookups). This leads to the lookup.py file being imported twice and the registration code executing twice. Thankfully this is no longer the default in Django 1.4. Keeping your import consistant to include the project name (when your app is included inside the project directory) will avoid these errors.

Extending imagestore models

I'm trying to install imagestore app in my project. Default models are created succefully, and all other parts working properly.
But, it have a feature to extend base (abstract) models and create your own by some complicated mechanism. This is mine model:
from django.db import models
from imagestore.models.bases.album import BaseAlbum
class Newalbum(BaseAlbum):
title = models.CharField("title", max_length=128)
class Meta(BaseAlbum.Meta):
app_label = "imagestore"
abstract = False
Also I have a string IMAGESTORE_ALBUM_MODEL = 'art.models.Newalbum' in my settings.
When I run syncdb it tells me Backend module "art.models" does not define a "Newalbum" class. ('module' object has no attribute 'Newalbum').
But of course it defined.
And the strangeness is only begins. When I put debug statement in the place where imagestore trying to get my model it prints proper module (already imported) and proper class name (string). But! dir(mod) prints only variables appeared before "from imagestore.models.bases.album import BaseAlbum" .In above example only "models" and default underscored attributes. Why? What I don't know about importing modules?
I already tried to install it in many awkward combinations of settings properties, versions of django (and required apps), app_label and so on. It creates tables, when I doesn't add IMAGESTORE_ALBUM_MODEL in my settings, but this models hasn't any BaseClass' behaviour.
So, what I'm doing wrong?
Solved it! While stepping through the import process (I added the following to imagestore.utils.load_class)
import pdb
pdb.set_trace()
I found that there is a circular import. Here's the order of things (slightly simplified):
load_class(IMAGESTORE_ALBUM_MODEL)
from imagestore.models.bases.album import BaseAlbum
BaseAlbum has an FK to IMAGESTORE_IMAGE_MODEL (head), so it gets imported with load_class
load_class(IMAGESTORE_IMAGE_MODEL)
from imagestore.models.bases.image import BaseImage
BaseImage has an FK to IMAGESTORE_ALBUM_MODEL (album), so it gets imported with load_class
When this calls __import__ IMAGESTORE_ALBUM_MODEL is already in sys.modules, though incomplete because it's still being built. And thus the module doesn't have an "Album" attribute yet.
The quick solution is to move the head field to models.album.Album and remove it from BaseAlbum. After doing this I was able to get the site running again. You must use the models package in your new app (not a simple models.py), with Image and Album in separate files.
If this isn't clear enough, drop me a line and I'll try to do better.

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.

import error when running django south

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...)