Django AppConfig ready() - django

I have Django 2.1.7
I read Django documentation as well as, this How to use django AppConfig.ready() and this Overriding AppConfig.ready().
Now, the folder/file structure is like this,
my_app
__init__.py
urls.py
sub_app1
migrations
__init__.py
admin.py
apps.py
models.py
...
sub_app2
...
sub_app3
...
and all sub_apps are registered into INSTALLED_APPS of Django settings.py file as my_app.sub_app1 for example. I noticed that when I try to override ready()for sub_app1 w/in sub_app1.apps.py, overriding has no effect.
When I flatten my structure above as
my_app
__init__.py
urls.py
apps.py
overriding of ready works fine.
In other words, it seems that overriding of readyof a sub app w/in sub app's apps.py doesn't work.
Also, whether using default_app_config w/in sub app's __init__.py or my_app.sub_app1.apps.SubApp1Config w/in settings.py INSTALLED_APPS it gives error django.core.exceptions.ImproperlyConfigured: Cannot import 'sub_app1'. Check that 'my_app.sub_app1.apps.SubApp1Config.name' is correct.
Why is this happening?

Related

Django apps sharing a model

I Django can 2 apps share 1 model, or 2 apps must define the same model inside. so app1 and app1 can have have same products model inside them for example?
Yes, app1 and app2 can share the same model. You need to import it wherever you want to use it.
Lets say your project structure is like below having 2 apps app1 and app2.
my_project/
manage.py
my_project/
__init__.py
settings.py
urls.py
wsgi.py
app1/
__init__.py
admin.py
migrations/
__init__.py
models.py
tests.py
views.py
app2/
__init__.py
admin.py
migrations/
__init__.py
models.py
tests.py
views.py
Then to use the models defined in app1/models.py in app2, you just need to do:
from app1.models import MyModel # import the model
Just import the model from the app that defines it to the app that is using it.

How do I customize the admin page in Django 1.7?

I've been learning from the Django Documentation and have stumbled upon a road block in the section: https://docs.djangoproject.com/en/1.7/intro/tutorial02/#customize-the-admin-look-and-feel
My files are arranged as:
mysite/
mysite/
__pycache__/
...
__init__.py
settings.py
urls.py
wsgi.py
polls/
__pycache__/
...
migrations/
...
__init__.py
admin.py
models.py
tests.py
views.py
templates/
admin/
base_site.html
db.sqlite3
manage.py
I have modified the text in the file base_site.html to "Polls App" but the admin site continues to display "Django Administration".
PS: I'm using Win8
You need to place that templates folder in the project directory, not on the same level as the project as it currently is.
So place it under mysite/templates and you should be close.
EDIT: Actually, put the templates folder under your application so that it is Polls/templates
I have it at the same place as you, right under the project root. Make sure that django.contrib.admin is defined before your polls app in INSTALLED_APPS.

Django recommended structure TastyPie

Using TastyPie (API framework for Django) what is the recommended file structure (or in your opinion).
Have a different resources in each app something like this:
app1
models.py
resources.py
app2
models.py
resources.py
Or create a new app say and have one resource one resource:
app1
app2
apiApp
resources.py
Here is very simple and efficient structure of tastypie api.
Projectname
app1
api
__init__.py
resource.py
models
__init__.py
model1.py
model2.py
migrations
__init__.py
__init__.py
app2
api
models
migrations
__init__.py
common
__init__.py
constant.py
authentication.py
commondata.py
middleware
__init__.pt
cors.py
projectname
__init__.py
settings.py
urls.py
wsgi.py
manage.py
Projectname.wsgi
Creating a new app for just resources does not make sense. I would go with having different resources in each app. Alternatively you can have them in /resource.

ImportError After Moving App to Nested Folder

My application was working fine when I wanted to see whether I could organize my project in a better way. I read through this tutorial on structuring a django project.
Before my project structure was as follows:
camucamu
books
admin.py
models.py
views.py
__init__.py
static
templates
urls.py
views.py
settings.py
wsgi.py
__init__.py
What I wanted to do was move the books app into an apps folder. Thus I did that and changed the project structure to the following:
camucamu
apps
books
admin.py
models.py
views.py
__init__.py
static
templates
urls.py
views.py
settings.py
wsgi.py
__init__.py
I then changed the imports in views.py and admin.py
from books.models to apps.books.models.
I also changed INSTALLED_APPS in settings.py from books to apps.books.
When I then tried to run syncdb, I get the following error:
raise ImproperlyConfigured('ImportError %s: %s' % (app, e.args[0]))
django.core.exceptions.ImproperlyConfigured: ImportError apps.books: No module named apps.books
What am I messing up here so it can't find my app anymore?
Your apps folder does not have an __init__.py file so it cannot be recognized as a python module
I got the same error, following the same guide, as the last point of the following list was not cited. Make sure you performed the following changes:
Create a blank __init__.py file inside the apps folder (needed for python to recognize it as a package)
Update the import statements wherever you refer to an external app:
from projectname.apps.appname.models import YourModel, YourOtherModel
Inside settings.py edit INSTALLED_APPS such that it looks like this:
INSTALLED_APPS = (
...
# apps
'projectname.apps.appname1',
'projectname.apps.appname2',
)
This one is not specified in the guide: In all your urls.py files, update the urlpatterns!
BEFORE:
# client views
urlpatterns += patterns('appname',
...
)
AFTER:
# client views
urlpatterns += patterns('projectname.apps.appname',
...
)
Finally remember to update your changes by calling python manage.py syncdb
Hope that helped.

Where should i create django apps in django 1.4?

I've just started a new project in django 1.4 and since they've changed the default layout for manage.py and the whole folder hierarchy (see https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py) i cannot decide where should i put my app packages - inside mysite or outside it? What's the best practice? For some reason, startapp command creates the app outside of the mysite package, but this feels somehow wrong.
So, what's the best? This:
manage.py
mysite/
__init__.py
settings.py
urls.py
myapp/
__init__.py
models.py
or this:
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py
?
The second way.
manage.py
myapp/
__init__.py
models.py
mysite/
__init__.py
settings.py
urls.py