I'm working on a web application with Django framework. I have this structure :
project
web_app
models
models.py
rwtool
settings.py
urls.py
views.py
I defined my models in models.py file. In my settings file I added this line project.web_app in INSTALLED_APPS. When I did
python mange.py syncdb
the models are not created. To create them I must add the project.web_app.models to my settings file. I have looked to others project and they don't do like this and it works. Is it correct what I'm doing?
models.py should be under your app folder (in your case "web_app").
If you want to separate your models then you have to turn models folder into a python package, in this case add an __init__.py in that folder and make sure that in the init file your models are accessible to the outside world at the path "web_app.models.ModelName".
So, solution 1 (standard Django approach):
project
web_app
models.py
Solution 2:
project
web_app
models
__init__.py
ModelOne.py
ModelTwo.py...
the __init__.py will contains something like:
from .ModelOne import ModelOne
from .ModelTwo import ModelTwo
The approach N2 can be also used for views and other stuff too ;)
First add your app to INSTALLED_APPS , and then run
python manage.py inspectdb > somefile.txt
You can get quickly check out if your database structure is matching your django models.
If python manage.py inspectdb > somefile.txt not created any structure, then make sure that your app is added to to INSTALLED_APPS.
Related
My project structure is:
main
settings.py and etc.
app1
views.py and etc.
app2
views.py and etc.
manage.py
I have decorators that I use in views at app1 and app2. Where should I locate file decorators.py? Options in my opinion:
create decorators.py in app1 folder and import this to at app1.views and app2.views
create decorators.py in app1 folder and import this to app1.views and create decorators.py in app2 folder and import this to app2.views.py (decorators.py will contains the same code, not so good)
create decorators.py in main folder and import this to at app1.views and app2.views
Your opinion?
Any choice will work. Which one is the best depends on the project and the relation between app1 and app2:
If they both are independent apps then you might consider choosing option 2 nevertheless.
If you have other dependencies between them, say app2 defines a post_save for a model in app1 or something like that then I would suggest option 1.
If they are more just logical compartments within your project and not actual standalone applications I would go for option 3.
To some extent this is also a matter of personal preferences.
Some further reading for the interested ones:
Django's Documentation about decorators
Working with Django View Decorators by simpleisbetterthancomplex
I think it makes sense to organize my Django apps in a different way (by protocol):
📂myapp
apps.py
models.py
utilities.py
📂html
admin.py
urls.py
views.py
📂rest
serializers.py
urls.py
views.py
📂graphql
etc
This would move the admin.py file into the html folder, but sadly autodiscover does not seem to find it anywhere else than in the myapp folder. Is there a way to point to the correct location: perhaps in apps.py? I am sure a symlink would work, but not really what I am after.
There's nothing magic about either admin.py or autodiscover. All admin.py does is run any register calls on import, and all autodiscover does is look for an admin.py file in each installed app and import it. There's nothing to stop you importing your admin.py in its custom location from somewhere else, eg the models or views files.
This was somewhat tricky, but the comment by #Joran Beasley set me in the right direction.
For admin.py to be autodiscovered it needs to be either in the top level app folder, or be imported in a file. However... not every file works.
If you do from .html import admin
into __init__.py or apps.py: django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
into models.py: Results in an importerror from models into admin. I think due to the circular nature of the imports.
However the following works: from . import admin inside the urls.py. Make sure to add a comment that this import is needed for autodiscover purposes.
Let's have a django project using a 3rd party application.
I'd like to override some of its modules without touching original files.
Simple subclassing is not possible here, need to override code transparently as many other apps rely on original class names and functions.
Project's structure looks like:
django_project/
__init__.py
settings.py
overrides/ <-- here is a subdir with apps overrides
__init__.py
payment/ <-- here is an example of app to override
__init__.py
admin.py
forms.py <-- this file is ignored, original is imported
models.py
tests.py
views.py
settings.py was modified with
INSTALLED_APPS=(
'satchmo_store.shop'
#'payment' # original values
'overrides.payment' # modified app
...
)
The above solution however does not work, because Django does not insert path of added app into modules search path (sys.path).
Django just loads admin.py, models.py, tests.py and views.py, other files like forms.py are ignored.
Is this behaviour documented somewhere ? What exactly placing a module name in INSTALLED_APPS does behind scenes ?
I hacked the situation with hardcoding new modules search path in manage.py and Apache's setting of WSGIPythonPath.
import os.path
import sys
DIRNAME = os.path.dirname(__file__)
APPS_OVERRIDE = os.path.join(DIRNAME, 'overrides')
if not APPS_OVERRIDE in sys.path:
sys.path.insert(1, APPS_OVERRIDE)
I doubt this is the right way. Cann't find a guide describing apps overriding.
So, how can I properly override external Django application in my project ?
The bonus question: Do I need to copy whole application directory tree, not just particular files which are really modified ? As far as I know, Python stops at first matching module path, so it won't import other modules available in following parts of the search path.
Example of how to override your form:
overrides/payment/forms.py
from django import forms
class YourNewFormThingy(forms.Form): pass
overrides/payment/models.py
from satchmo.payment import forms as satchmo_payment_forms
from . import forms
satchmo_payment_forms.SomeForm = forms.YourNewFormThingy
Try including payment as well along with override.payment as satchmo uses payment module to process payments and payment code is flexible enough to include your code as well.
I am using Django wiki module in my project , IN wiki module models are created in their different files.Structure are as follows.
models/
article.py
abcd.py
...
__init.py__
Now I want to add some new models into article.py but after creating models when I try to migrate them I am getting the message , Nothing seems to change. However If I change any field of existing model then migration catch the change.
My init.py files imports all models of articles.py , So I think migration must have created the models but It doesn't . so can anyone tell me where I am wrong.
Thanks
Is it possible to have a set of models just for testing purposes? The idea is that I've written an app that contains some helper abstract model HelperBase. Now I'd like to provide some models that would inherit from it in order to test it, say DerivedTest1, DerivedTest2. However I wouldn't really like those test models to appear in the production database in the end. I just want their tables to be constructed in the test database. Is it possible and if so - how to do it? I've already tried creating models in the tests.py file but this doesn't seem to work.
You could try creating a whole new app that you only use on your development server.
E.g., if your app is called myapp you would call your testing app myapp_test.
Then in myapp_test's models.py you would from myapp import models and then subclass your models in there.
Then in your settings.py you either just try and remember to comment out the myapp_test application from INSTALLED_APPS when deploying to your production server. Or you can use the local_settings.py methodology to only have the myapp_test included in INSTALLED_APPS on your test machine.