I’m getting started with Django. I can’t get the admin to work (it used to work, I’m not sure what update made it break…).
As soon as I register a model in the admin, the website crashes with this error on any URL:
'module' object is not iterable
In the trace it happens to bug on this:
/Library/Python/2.7/site-packages/django/contrib/admin/sites.py in register
for model in model_or_iterable:
admin_class
<class 'django.contrib.admin.options.ModelAdmin'>
options
{}
model_or_iterable
<module 'model.Branch' from '...../farmpower/src/model/Branch.pyc'>
self
<django.contrib.admin.sites.AdminSite object at 0x1098196d0>
I’ve tried with different models, in that example, with Branch (code in admin.py):
from django.contrib import admin
from models import *
admin.site.register(Branch)
models.py:
import Page, Promotion, Branch, Contact
Branch.py
from django.db import models
from django.utils.translation import ugettext_lazy as _
class Branch(models.Model):
name = models.CharField
[…]
class Meta:
app_label = "model"
db_table = "cms_branch"
def __unicode__(self):
return self.name
Thank you for your help !
There are several things in your code that are not very neat. One of them might lead to the error you’re seeing, though I don’t know which one of ’em is it.
You use relative imports (
from models import ...). It is more robust do do an absolute import likefrom yourapp.models import ...).You use a “star import”:
from models import *. You don’t really know what you’re importing exactly in the file where you’re doing this. Also automatic code checkers (like pyflakes) cannot check whether you’re missing imports anymore.You mention
models.ymlas the filename. That’s not a.pyextension, so python doesn’t do a thing with that one.The app name you set in the
Metaon your models ismodel. Note that “models” and “model” are quite django-internal names. So having an app called “model” with a “models.py” could easily go wrong. Why is the app notyourappor something like that?Here are some ways in which it could go wrong:
Star import: perhaps you’ve imported a different
admininmodels.py? Which overwrites, through the star import, the one in admin.py?The
models.yml: if that really is the name, what doesfrom models import *return? With a non-existingmodels.py? Try to importBranchexplicitly:from models import Branchand see if it fails.App name: if your app is really called “model”, a relative import
from models import *could perhaps get you the top-level “model” module instead of the “model.models” that you mean in case you mis-type something.