How do you arrange model files and controller files in their respective folders?
And what do you have to write in the __init__.py file?
Currently I have all my models and controllers in the root folder of the module like this
addons\
-->mymodule\
-->views\
-->view.xml
-->__init__.py
-->__openerp__.py
-->models.py
-->controllers.py
I have tried like this
addons\
-->models\
-->models.py
And then import the models.py using this inside __init__.py
from models import models
But this does not work
addons\
->yourmodule\
->controllers\
->__init__.py
->controllers.py
->models\
->__init__.py
->modelname.py
->__init__.py
->__openerp__.py
Content of the init.py in the controllers folder:
from . import controllers
Content of the controllers.py in the controllers folder:
from openerp import http
Content of the init.py in the models folder:
from . import modelname
Content of the init.py in the module folder:
from . import controllers
from . import models
Content of the openerp.py in the module folder: List of all your xml files (Instruction)
Related
I have organized Python files in differnt folders:
root_dir
utils/
some_file.py
models.py
In some_file.py I just did this:
import models
But this is not working. I also updated some_file.py with:
file_dir = os.path.dirname(__file__)
sys.path.append(file_dir)
import models
Still i get:
import models
ModuleNotFoundError: No module named 'models'
Try to import it as follows from within the some_file.py:
import sys
sys.path.append('..')
import models
# Use as follows:
# models.function()
I have a folder Project which contains Django project called djangodb and Scrapy project called scrapyspider.
So it looks like:
Project
djangodb
djangodb
myapp
scrapyspider
scrapyspider
spiders
items.py
scrapy.cfg
__init__.py
I want to import model Product from myapp app in items.py
The problem is that it returns import error:
from Project.djangodb.myapp.models import Product as MyAppProduct
ImportError: No module named djangodb.myapp.models
Tried many things but couldn't avoid this error. Do you have ideas?
Your problem is that you're trying to do an import from a file that's outside from Django schema, to solve that you can overwrite the sys.pathvar which includes locations as the actual dir, so you can change it to:
import sys
sys.path.insert(0, 'C:\\Users\\your_path\\Project')
sys.path.insert(0, '/path/to/application/Project/') # Linux
# And then import #
from djangodb.myapp.models import Product as MyAppProduct
I'm following the directions from the first answer here:
Django: split views.py in several files
I created a 'views' folder in my app and moved my views.py file inside and renamed it to viewsa.py. I also created an init.py file in the 'views' folder.
My folder structure:
myproject/
myproject/
...
...
myapp/
__init__.py
urls.py
views/
__init__.py
viewsa.py
The first problem is in myapp/views/init.py I tried to do this:
from viewsa import *
and I get an "unresolved reference viewsa" error
I can however do this in the same file instead (in other words, it doesn't throw an error):
from . import viewsa
But I can't find any way to import these sub directory views into myapp/urls.py even following the directions in the link above. What am I doing wrong?
Use relative import, in your __init__.py:
from .viewsa import *
(notice the dot in .viewsa)
Lets say I want to build a project Facebook
I need a project structure like
facebook/
__init__.py
feed/
__init__.py
models.py
business.py
views.py
chat/
__init__.py
models.py
business.py
views.py
games/
__init__.py
models.py
business.py
views.py
common/
common.py
runserver.py
How can I structure this well so that when I run
python facebook/runserver.py
It loads views from all my apps internally?
I want to keep this structure because extending the project further is more natural way
I am trying to follow their advice, but don't really understand where I need to write
from flask import Flask
app = Flask(__name__)
and how to import all views from all apps at one place, please help
If lets say I write the above code in facebook/__init__.py, then how in facebook/feed/views.py, I can do
from facebook import app
Use blueprints. Each one of your sub-applications should be a blueprint, and you load every one of them inside your main init file.
Answering your second question
from flask import Flask
app = Flask(__name__)
You should put this into facebook/__init__.py
BTW, my runserver.py and settings.py always resides one level under facebook/.
Like this:
facebook/
__init__.py
feed/
__init__.py
models.py
business.py
views.py
chat/
__init__.py
models.py
business.py
views.py
games/
__init__.py
models.py
business.py
views.py
common/
common.py
runserver.py
settings.py
Content of runserver.py:
from facebook import app
app.run()
I suppose the content of settings.py should not be explained.
Content of facebook/__init__.py:
from flask import Flask
app = Flask(__name__)
app.config.from_object('settings')
from blog.views import blog #blog is blueprint, I prefer to init them inside views.py file
app.register_blueprint(blog,url_prefix="/blog")
I have tried blueprints and came up with a solution which works for me, let me know if you have other ideas.
Project Structure
facebook/
runserver.py
feed/
__init__.py
views.py
chat/
__init__.py
views.py
Code
# create blueprint in feed/__init__.py
from flask import Blueprint
feed = Blueprint('feed', __name__)
import views
# create blueprint in chat/__init__.py
from flask import Blueprint
chat = Blueprint('chat', __name__)
import views
# add views (endpoints) in feed/views.py
from . import feed
#feed.route('/feed')
def feed():
return 'feed'
# add views (endpoints) in chat/views.py
from . import chat
#chat.route('/chat')
def chat():
return 'chat'
# register blueprint and start flask app
from flask import Flask
from feed import feed
from chat import chat
app = Flask(__name__)
app.register_blueprint(feed)
app.register_blueprint(chat)
app.run(debug=True)
In Action
* Running on http://127.0.0.1:5000/
# Hit Urls
http://127.0.0.1:5000/feed # output feed
http://127.0.0.1:5000/chat # output chat
First django app and am having a bit of trouble my project is laid out like this
MyProject
-dinners (python package, my app)
-views (python package)
__init__.py
dinners.py(conflict was here... why didn't I add this to the q.. sigh)
general.py
__init__.py
admin.py
models.py
tests.py
views.py
-Standard django project boilerplate
In my /views/general.py file it looks like this:
import os
import re
from django.http import HttpResponse
from django.template import Context,loader
from dinners.models import Dinner
def home(request):
first_page_dinners = Dinner.objects.all().order_by('-created_at')[:5]
t = loader.get_template('general/home.html')
c = Context({
'dinners':first_page_dinners,
})
return HttpResponse(t.render(Context()))
And in my urls.py file I have this regular expression to map to this view
url(r'^/*$','dinners.views.general.home', name="home")
However when I try to hit the home page I get this error:
Could not import dinners.views.general. Error was: No module named models
Removing the dinners.models import at the top of general.py (plus all of the model specific code) removes the error. Am I somehow importing incorrectly into the view file? Naturally I need to be able to access my models from within the view...
Thanks
UPDATE: answer I had a file dinners.py within the -views package that was conflicting
You need to put a __init__.py file in "dinners" and "views" folder to make those valid packages.
EDIT:
Also, remove that views.py file, that will create conflict with the package.