Django path error - No module named models - django

I have an import error in a Django app, I guess it's pretty simple but I can't figure it out…
It's a simple notepad app, called «notes» with 2 models, «note» & «category». To clean things up, I wanted to seperate each models views & urls, so here is what I get:
/notes
admin.py
forms.py
models.py
/urls
category.py
note.py
/views
category.py
note.py
The problem is that my views don't find the models, here is the Traceback - http://dpaste.com/hold/539425/ and parts of the code: http://dpaste.com/hold/539416/
I didn't mention the _init_.py files in the above snippet, but I double-checked all of them…

Your code pastie says from notes.models import Category, Note but your traceback says from models import Category, Note. It's hard to tell if those are the same line because you're only pasting snippets. Still, it should be 'from notes.models import foo' always.
Are you sure you're always referencing the notes app in your models import?
UPDATE:
If you're using the correct from foo.models import bar syntax, then my next thoughts are that you've possibly got a circular dependency that's stopping Python importing things from the models file.
But the traceback implies the notes/models.py can't be found. So... when you say you double-checked the init.py files, that doesn't mean they're right. :p There should be one in notes/ and also in notes/urls/ -- they don't have to contain anything.

It should be
from notes.models import Category, Note

Related

How Should i import the Model Class on django to other py file to use it?

I wanted to insert model instance to database from a file. Ι wrote a python program to do so by importing models but every time Ι get this error:
ModuleNotFoundError: No module named 'exams'
I have tried putting the file in the same folder but then I got:
"Relative import error"
the expected result was to save model instance in the database.
You need to include more details, at least your files' paths. But if we assume that both models.py and I guess views.py files are in the same folder you should use:
from .models import exams
Unless what you are trying to import is the django models. In that case you use:
from django.db import models
As I said your questiong is poorly written and needs improvement to better understand your problem.

Django unable to load model into views

I am trying to import my models into views.py but I am unable to do so. However I am able to register them on the admin site but when I use the same code I used in admin.py to import the models into views.py, I get an error. I am using djongo so I am not sure if that changes anything about how to import them and I cannot seem to find the documentation for it.
models.py
from djongo import models
class Round(models.Model):
round_num = models.IntegerField(default=0)
admin.py
from django.contrib import admin
from .models import Round
admin.site.register(Round)
views.py
from .models import Round
When I try and run my views.py file I get the following error: ModuleNotFoundError: No module named 'main.models'; 'main' is not a package
Also my views, admin, and models file are all in the same directory. I have made the migrations and I can see my Round model in MongoDB. The only thing I cannot do is import it to the view
You need to have an __init__.py file in your directory. It should be inside of your main folder and at the same level as your views.py and models.py
As a workaround, since the models properly migrate to MongoDB. Using pymongo I have just connected to Mongo and have rendered data into my views this way. It works fine so if anybody else has an issue loading in their models, you can always just connect directly to the DB.

Django signal receiving across apps

I have a main_app, and app2. main_app is essentially a list of items with data, and app2 has more information about said items.
main_app isn't supposed to know about app2, but app2 can import from main_app.
Within main_app/signals.py, I have
import django.dispatch
mysignal = django.dispatch.Signal(providing_args=['uid'])
In main_app/views.py, I have a view which renders various main_templates, containing the details of the item, a view for editing, and one for submitting said edited data. The idea is that a signal is sent when each of those is called and, app2 receives this. main_template uses the "with" call to get template2 and that app's information.
In app2/processes.py I have the following:
import django.dispatch
from django.dispatch import receiver
import my models
from main_app.signals import mysignal, (mysignal2, etc)
Then for each method, I have
#receiver(mysignal)
def foo(sender, **kwargs) etc
OK... So, in main_app/views.py, if I have the line:
from app2.processes import mysignal, mysignal2 etc
Everything works smoothly. But I want to remove any reliance on app2 in main_app. As far as I am concerned, app2 is just importing these signals from main_app/signals.py.
But if I try to get rid of the above line and put the following into main_app/views.py
from main_app.processes import mysignal, my...
It doesn't work...I don't get an error but the data from app2 doesn't render into the template, and I really don't see why....Any ideas?
Your signal receivers in app2 are probably not registered. Simple check: put raise Exception("I was imported!"); as the first line in app2/processes.py. You probably will never see that exception. You will have to make sure the signal receivers are being registered. You can do this by import app2.processes somewhere Django will look. For example in app2/models.py.

Django 1.5.1 - Admin.py missing while running startapp

I've been following the DjangoProject tutorial. When I run python manage.py startapp newapp while in the same directory as manage.py. In the newapp directory I see init.py, models.py, tests.py, and views.py but not admin.py file. Where is admin.py?
I am running Django 1.5.1 in Windows 8
You have to create an admin.py file.
you don't necessarily need an admin.py file,
just import the admin module in your models.py file,
from django.contrib import admin
and for each model do the following:
admin.site.register(model1)
admin.site.register(model2)
However, this is not best practice, but since it's just a tutorial, it will work.
You also need to uncoment the relevant lines in the urls.py file
I think I had the same frustrations following the DjangoProject tutorial - however, when I cross-referenced it with with the DjangoBook tutorial (for the same version, I believe, 1.5.1), I found that an admin.py file was not necessarily created after a python manage.py startapp xyz command -- moreover, I also uncommented all of the admin options in urls.py, views.py, and settings.py - so a bit of a mix of what Neal and Ibrahim said
You have to create your own admin.py file in the app if you want it. Indeed, this file is optionnal and isn't created by startapp.
If you want a default template to begin your admin.py, it should be:
from django.contrib import admin
from models import Model1, Model2
class Model2Admin(admin.ModelAdmin):
list_display = ('title', 'content', 'date')
# Just an example, chekc docs and tutorials for more info.
admin.site.register(Model1)
admin.site.register(Model2, Model2Admin)
The reason there is no default admin.py is because you don't have any models yet when you create your new application; so there is nothing to add to the admin section.
Further, you may not want to admin all the models in your application; or you may be creating an application that does not need any admin hookups; or you may not be using the admin application at all in your project.
Since django cannot decide this for you, there is no default admin.py generated.
To create one, if you are following the tutorial - simply keep reading and in part two you'll create the admin.py file when you learn about the admin contrib app and how to integrate it with your custom models.

django inspectdb in admin site (admin.site.register)

I have been given a small legacy db (mysql) to work with, and therefore, I have tried to use the inspectdb command in django.
So, I use something like:
python manage.py inspectdb > models.py
It works fine, and I take the models.py and copy the class it created into my app (searchapp) as a models.py file.
I have done a syncdb, and everything went fine. Results are as expected.
Now, I have tried to add this model into my admin site, using:
from searchapp.models import Abaqus
from django.contrib import admin
admin.site.register(Abaqus)
stored in a file called admin.py (Abaqus is the name of the class generated by inspectdb and searchapp is the app name). However, I am not able to see the app on the admin site. I have checked my settings.py and it includes the 'searchapp'.
I was wondering if someone could point me in the right direction..
I suspect admin.py is not loaded. You could check this by putting a debug statement just above the register call:
import pdb;pdb.set_trace()
admin.site.register(Abaqus)
If this is in fact the case, the correct way to ensure admin.py is loaded is to call django.contrib.admin.autodiscover() at the beginning of your main url conf.
If you've written no admin classes and don't want an admin.py, you can call admin.site.register(Abaqus) right below where the model is defined, inside models.py.
If you have an admin module structured like the following, import admin within models.py to ensure the code is run:
myapp
|--models.py
|--views.py
|--admin
|--__init__.py
|--views.py
|--base.py
Another possible cause would be that you are missing the permissions to edit the model. Double check that you are logged in as a superuser.