packaging a django project with multiple apps - django

am trying to get my head around how to package a django project which contains number of apps. For that, I started a new project with following structure
PROJECT_PACKAGE
++ APPS
++++++ APP_A
++++++++++++ views.py
++++++++++++ models.py
++++++++++++ urls.py
++++++ APP_B
++++++++++++ views.py
++++++++++++ models.py
++++++++++++ urls.py
manage.py
urls.py
wsgi.py
then, I created new project and added the above structure as symbolic link in the python site-package. In my new django project, I went to the INSTALLED_APPS and added above project name.
I did not get errors on include, but when I try to view the model structure for APP_A
./manage.py sql APP_A
I was getting error that
App with label shopler is missing a models.py module.
what is he best way to include all my apps models and be seen each as independent app? is it possible?

It sounds like your tried to add the entire first project as a symbolic link inside of the second project. That would be a problem.
A straightforward way to do what your want would be to add symbolic links for the individual apps of the first project inside of the second project. So you'd end up with something like:
Project 1/
App A/
models.py
...
App B/
models.py
...
Project 2/
App A [link]
App B [link]
And then in Project 2's INSTALLED_APPS, list the apps individually.

Related

How to add a sub-app in django to INSTALLED_APPS?

I have the structure of my project:
project
———— project
———— app
———————— subapp
———— manage.py
I added my sub-app to INSTALLED_APPS like that:
INSTALLED_APPS = ['app.apps.AppConfig', 'app.subapp.apps.SubapConfig']
But it doesn't works. Django gives me an error message:
No module named 'news' and Cannot import 'news'. Check that
'apps.subapp.apps.SubapConfig.name' is correct.
Let's assume I've created an app named polls with the following command:
python manage.py startapp polls
Now I want to create a sub-app named subpoll inside the main app means inside the polls folder. So, how do I do that?
At first, I'll create a directory My_App_Name inside the /polls folder. In my case, I'll create a subpoll folder inside the polls folder.
After that, run the following command to create the new app.
python manage.py startapp My_App_Name ./polls/Your_Apps_Folder_Name/
So, in my case the following command will be like:
python manage.py startapp subpoll ./polls/subpoll/
Finally, I'll add just created apps name in the settings.py like below:
INSTALLED_APPS = [
...,
'polls',
'polls.subpoll',
]
I had a similar issue. In the apps.py Config class you need to set name to be the full dotted path. So based on the structure you give:
project
———— project
———— app
———————— subapp
———— manage.py
You would have:
class AppConfig(AppConfig):
name = "app"
and
class SubappConfig(AppConfig):
name = "app.subapp"
This should then work for you.

Import relative apps in Django

I'm new to Django and have a question about app structure and imports. My project structure looks like this (from deploydjango.com):
root/
manage.py
mysite/
__init__.py
urls.py
wsgi.py
settings/
apps/
__init__.py
profile
__init__.py
models.py
views.py
video
__init__.py
models.py
views.py
photos
__init__.py
models.py
views.py
Basically I will have some profiles on my site, where each profile then have a number of uploaded photos and videos. So in my video model I have the following code:
from django.db import models
from XXXXX import Profile
class Video(models.Model):
# more fields
company = models.ForeignKey(Profile, related_name='videos')
How do I import the Profile model for use in the Video model? Or is my structure not suitable..? What would be best practice for this?
I think your structure is fine. Basically, python's modules are just a python file which you may import like this: import <filename> and you will have its functionality imported. But when you group some python files in a folder and include in that folder a file called init.py there you have a python package. With is a better way to manage a group of modules and it allows the dot notation in your imports. Django apps behave like packages as you may see.
If you want to import Profile within your video module then you have to do this:
from profile.models import Profile
That is, assuming that the Profile model is defined in your profile app in models.py. That should do.
If you want more information about python project structure this is a great place.

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 - How to add admin.py in my app folder?

I need to add admin.py in my app folder.
Currently the app folder contains the following files:
__init__.py
models.py
test.py
views.py
Any help will do :-)
The tutorial say to edit admin.py in the polls directory. However, if you're just starting a new project admin.py will not exist.
You can simply create a blank admin.py file and add the contents suggested by the tutorial. Django will automatically notice any admin settings from this file when the admin app is enabled.

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.